in reply to Re: Speeding permutation counting
in thread Speeding permutation counting

I thougth a hash might be faster, but no.

Rate hash if hash 9329/s -- -11% if 10473/s 12% -- Rate hash if hash 9436/s -- -11% if 10599/s 12% -- Rate hash if hash 9341/s -- -12% if 10662/s 14% --

Benchmark code:

A hash is definitely cleaner, though.

Replies are listed 'Best First'.
Re^3: Speeding permutation counting
by dsheroh (Monsignor) on Jul 18, 2007 at 18:38 UTC
    Cool... Guess I should've benchmarked my suggestion instead of just assuming it would be slower than the hash-based ideas.

    Actually, unless I've severely mis-benchmarked it, it appears that my modest optimization (combined with blokhead's suggestion of using chop) even outdoes BrowserUK's bitwise version and still more so with fewer lines (based on BrowserUK's test code, I'm comparing every pair of lines):

    100 lines/100 iterations: Rate bitwise ifchop bitwise 33.1/s -- -41% ifchop 56.5/s 71% -- 10 lines/10000 iterations: Rate bitwise ifchop bitwise 3226/s -- -57% ifchop 7463/s 131% -- 1000 lines/5 iterations: s/iter bitwise ifchop bitwise 3.06 -- -23% ifchop 2.37 29% --
    (I actually suspect I'm doing something wrong in my test because I don't see why the number of lines would affect the relative performance at all. Update: I was doing something wrong. I forgot to pull the strings out of the array and was just comparing indexes in my version. With my version fixed to actually do the comparisons, the bitwise version is much faster, as I had initially expected:
    Rate ifchop bitwise ifchop 4.22/s -- -87% bitwise 33.3/s 690% --
    )

    Benchmark code: