in reply to Re^2: Why is Windows 100 times slower than Linux when growing a large scalar?
in thread Why is Windows 100 times slower than Linux when growing a large scalar?

The problem seems to be pretty definitely sourced within the MS CRT.

If I compile and run this using gcc under Ubuntu running inside a VirtualBox emulator (which ought to carry some overheads):

#include <stdio.h> #include <stdlib.h> #include <time.h> int main( int argc, char** argv ) { long long i, n = 1000000; char *p = (char*)malloc( 1000 ); time_t start, finish; double elapsed; time( &start ); for( i = 2; i < n; ++i ) { // printf( "\r %lld\t", i * 1000 ); if( ! ( p = (char*)realloc( p, 1000 * i ) ) ) { printf( "\nFailed to realloc to %lld\n", i * 1000 ); exit( 1 ); } } time( &finish ); elapsed = difftime( finish, start ); printf( "\nfinal size: %lld; took %.3f seconds\n", n * 1000, elaps +ed ); exit( 0 ); }

It takes less that a second to realloc a buffer to 1GB in 1000 byte increments:

mehere@mehere-desktop:~$ gcc mem.c -o memtest mehere@mehere-desktop:~$ ./memtest final size: 1000000000; took 0.000 seconds

However, if I compile and run this using MS VC++:

#include <stdio.h> #include <time.h> int main( int argc, char** argv ) { __int64 i, n = 1000000; char *p = (char*)malloc( 1000 ); time_t start, finish; double elapsed; time( &start ); for( i = 2; i < n; ++i ) { // printf( "\r %I64d\t", i * 1000 ); if( ! ( p = (char*)realloc( p, i * 1000 ) ) ) { printf( "\nFailed to realloc to %I64d\n", i * 1000 ); exit( 1 ); } } time( &finish ); elapsed = difftime( finish, start ); printf( "\nfinal size: %I64d; took %.3f seconds\n", n * 1000, elap +sed ); exit( 0 ); }

it takes over an hour to run. I haven't had the patience to let it complete yet!

I also compiled it with MinGW (which appears to also use the MSCRT?), and it has taken 1hr 20 mins (so far and appears to only 1/4 of the way there).

The problem seems to lie with the CRT realloc() which grows the heap in iddy-biddy chunks each time.

In addition, it may be

Bottom line: The MSCRT heap management routines are crap!


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
RIP PCW It is as I've been saying!(Audio until 20090817)

Replies are listed 'Best First'.
Re^4: Why is Windows 100 times slower than Linux when growing a large scalar?
by ikegami (Patriarch) on Dec 01, 2009 at 07:25 UTC
    Is the Perl inside the virtual box also threaded?

    Obviously, that makes no sense. I sleep now.

      Yes, it is a threaded perl under linux and threads work there fine.

      A perhaps more interesting question is if perl is built on win without USE_IMP_SYS so that USE_PERL_MALLOC can be enabled, does that stop you from using threads? Or just fork?

      Whilst I would miss the piped open, which i believe requires the fork emulation, I could work around it using threads. And I would infinitely prefer threads + faster memory, to the fork emulation.

      Does anyone know the answer or should I just suck it and see?


      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
        does that stop you from using threads? Or just fork?

        On a second reading, it seems that you might only miss out on fork emulation. One *can* build perl with PERL_MALLOC, USE_MULTI and USE_ITHREADS, but without USE_IMP_SYS (and I've now just done that) ... which, according to the comments in the makefile.mk/Makefile, would mean that you miss out on fork emulation, but not threading ... and that's exactly what I'm finding:
        C:\_32\comp\perl-5.11.2>perl -Mthreads -e "print \"ok\"" ok C:\_32\comp\perl-5.11.2>perl -e "fork()" The fork function is unimplemented at -e line 1.
        (And I still get good results regarding the concatenation operator.)

        Existing ppm packages for extensions would, according to the comments in the makefile.mk/Makefile, be unusable on such a perl.

        Cheers,
        Rob