[racket] why does in-indexed / in-range slow down my program?

From: Matthew Flatt (mflatt at cs.utah.edu)
Date: Mon Jan 21 00:22:13 EST 2013


At Sun, 20 Jan 2013 15:25:12 +0800, Limbo Peng wrote:
> As the profiler shows, the most time-consuming part lies in the for-loop of 
> procedure "qf-connect!", and after a few experiments, I figure it out: it is 
> the "in-indexed" procedure that makes it slow. I replaced "in-indexed" with 
> separate calls to "in-range" and "in-vector" - the running time gets reduced 
> from 4m to 27s. Why?

The problem wasn't the presence of `in-indexed' but the absence of
`in-vector'. If you write

       ....
       (for ([(v i) (in-indexed (in-vector items))])
          ....

then performance should be the same as using `in-range' plus `in-vector'.


At Sun, 20 Jan 2013 18:21:51 -0500, Matthias Felleisen wrote:
> I can confirm a serious speed-up with the explicit vector-ref inside
> the for loop instead of the for-loop lookup with (in-vector ...). I
> get from 20s to 12s on my reasonably new (small) macair.

I wasn't able to replicate that difference, so far. I can't see why it
would happen, either.


All of the variants (including `(in-indexed (in-vector ...))' run in
about 15s in my machine using `racket' on the command line. (The Go
version runs in 4s on my machine.)

Some variants are faster or slower for me, but only by a second or two.
For example,

At Sun, 20 Jan 2013 17:11:02 -0700, Danny Yoo wrote:
> It also seems to be that using:
> 
>     (in-naturals)
> 
> instead of:
> 
>     (in-range n)
> 
> improves the performance a bit more: on my system, that single change
> cuts down the runtime from about 22 seconds to about 15 seconds.

It goes from 16s to 14.5s on my machine. I think the difference is that
`in-naturals' doesn't have to check for whether it's reached `n'. The
`(in-indexed (in-vector ...))' combination is the same on my machine as
using `in-naturals'.


At Mon, 21 Jan 2013 11:33:00 +0800, Limbo Peng wrote:
> 1. how can I check the expansion of the for-loop? I'm using the Racket REPL 
> from CLI.

Use the `raco expand' command.

> 2. I've noticed that the let-form in my code is replaced by "define", is this 
> the preferred coding style?

Yes.



Posted on the users mailing list.