Performance on Windows: Cygwin is much faster. Why?

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

•  Subject
• Author
• Posted on
Below is a small program to calculate some values using Lehmer's algorithm.
Running it on Windows XP, I found some rather strange performance facts.

Using ActiveState 5.10, it runs in 1117 seconds.

With Strawberry 5.10, it takes 945 seconds.

But with Cygwin (using version Perl 5.8), on the same machine, it finishes
in 488 seconds, it is twice as fast!

These results are reproducible.

Why would Cygwin be faster by such a large margin? Is it because of the
underlying libraries being more efficient or am I overlooking something?

Following, the program:

# Given: G(n) = ( 7**5 * G(n-1) ) % ( 2^^31 - 1 )
# Find : X = G( 943683858) = 133481
#        Y = G(1657543960) = 447352

\$| = 1;

my \$starttime = time();

my \$n = 0;
my \$g = 555;

my \$multiplier = 7**5;
my \$mask       = 2**31 - 1;

for (; \$n < 943_683_858; \$n++) {  \$g = (\$multiplier * \$g) % \$mask; }

my \$seconds = time() - \$starttime;
print "Found X=\$g in \$seconds seconds, ";

for (; \$n < 1_657_543_960; \$n++) { \$g = (\$multiplier * \$g) % \$mask; }

\$seconds = time() - \$starttime;
print "Y=\$g in \$seconds seconds.\n";

# END OF PROGRAM

Re: Performance on Windows: Cygwin is much faster. Why?

Can you test this with Cygwin 5.10 and Strawberry 5.8? I suspect the
difference is more likely to be a 5.10 slowdown than a Cygwin speedup;
if this is the case, it should probably be reported to p5p.

Also, can you post the results of

perl -MDevel::Peek -e"Dump 1_657_543_960 * 7**5"

with each perl? If some of your perls have 32bit and some 64bit
integers, that is likely to make a difference to performance.

Ben

--
Razors pain you / Rivers are damp
Acids stain you / And drugs cause cramp.                    [Dorothy Parker]
Guns aren't lawful / Nooses give
Gas smells awful / You might as well live.                   ben@morrow.me.uk

Re: Performance on Windows: Cygwin is much faster. Why?

Sure, I'll try that tomorrow.

Can't do it for ActiveState right now, but:

==========
Strawberry
==========

perl -v says this:
This is perl, v5.10.0 built for MSWin32-x86-multi-thread

The output you requested:

SV = NV(0x9d59fc) at 0x9b98b4
REFCNT = 1
NV = 27858341335720

======
Cygwin
======

perl -v says this:
This is perl, v5.8.8 built for cygwin-thread-multi-64int

The output you requested:

SV = IV(0x10029318) at 0x10010fc0
REFCNT = 1
IV = 27858341335720

I'm not sure what the output means, but I see that the version under Cygwin
talks about 64int. Perhaps this (as you mentioned) point to using 64-bit
integers? I can see why that would speed things up. However, I'm guessing
here.

This all runs on an Intel Core Duo processor, btw, not sure if it's
relevant.

Dutch

Re: Performance on Windows: Cygwin is much faster. Why?

This means some of your intermediate results are being stored as NVs,
that is, as C doubles, because they are too big to fit in an IV, which
in this case is a 32bit C long.

This means that all intermediate results are IVs, which in this case
are 64bit C long longs. Integer arithmetic is much faster than floating-
point arithmetic (and, probably, arithmetic that has to keep
converting from one to the other is slower than either), so that's why
cygwin is faster.

AFAICT, it is not possible to build a perl for Win32 with the
equivalent of use64bitint, the way the cygwin perl is built, so if you
need speed, either get a 64bit processor and OS and build a 64bit perl
or stick to cygwin.

Ben

--
#!/bin/sh
quine="echo 'eval \$quine' >> \\$0; echo quined"
eval \$quine
#                                                        [ben@morrow.me.uk]

Re: Performance on Windows: Cygwin is much faster. Why?

I can understand how this works, thanks for the explanation.

The performance isn't a problem, I just thought it was rather strange that
one was twice as fast on the same hardware. I will be migrating away from
Windows anyway (unrelated to this issue).

Thanks for teaching me something about the inner workings of Perl! ;-)

I'm guessing the benchmarks with the different versions of Perl on Windows
aren't necessary anymore as the mystery is solved. However, if anyone really
wants to see the results, I'll do it and will post here. Just let me know.

Dutch

Re: Performance on Windows: Cygwin is much faster. Why?

5.10 seems to be a bit slower, but not much:

5.8.8 as included in Debian Etch:
Found X=133481 in 325 seconds, Y=447352 in 573 seconds.

5.10.0 (compiled from source with default options):
Found X=133481 in 343 seconds, Y=447352 in 597 seconds.

(both run on the same system:
Intel(R) Core(TM)2 CPU          6300  @ 1.86GHz)

Found X=133481 in 139 seconds, Y=447352 in 245 seconds.

(Intel(R) Xeon(R) CPU           X5355  @ 2.66GHz)

So 64 bit at 43% higher clock rate seems to be 134% faster than 32 bit -
quite impressive (but it's a different CPU, too - so that might be
deceptive)

hp

Re: Performance on Windows: Cygwin is much faster. Why?

Both of these were built with 32bit IVs (why on earth do Debian do that?
The stock FreeBSD perl is built with 64bit IVs, even on a 32bit
system...)

<snip>

This perl was presumably built with 64bit IVs...

...so, again, you're comparing FP to integer arithmetic.

Ben

--
Razors pain you / Rivers are damp
Acids stain you / And drugs cause cramp.                    [Dorothy Parker]
Guns aren't lawful / Nooses give
Gas smells awful / You might as well live.                   ben@morrow.me.uk

Re: Performance on Windows: Cygwin is much faster. Why?

Yes.

Presumably because its the default and the Debian maintainer didn't see
a strong reason to change it. (I didn't, either - the 5.10.0 I compiled
myself also uses 32 bit IVs)

Yes. It's on a 64bit system.

Yes. I still think it's impressive. I would have expected the difference
between FP and integer arithmetic to be dwarfed by the interpreter