Tech Off Post

Single Post Permalink

View Thread: Why does my C program run faster on Linux than on Windows?
  • User profile image
    Shining Arcanine

    TimP wrote:
    

    So it is indeed doing dynamic linking. Maybe passing shared objects to gcc implicitly tells it to link them dynamically, but I've never seen it done before (I usually see -l<lib>).

    Since the running time seems to grow with the size of the input, my only guess is that threading is being (ab)used in a way that pthreads handle more gracefully than Windows threading (assuming pthreads-win32 is a wrapper for Windows threads). I would attempt to run to core calculations of the algorithm without any threading involved and compare the numbers. I'm not sure what you're using to do your timing, but there is a time program standard on Linux that you can use.

    time ./a.out



    I see you edited your post. Anyway, I am using time.h to do timing. I reran my program using Linux's time program and here is the output:

    richard@richard-desktop:~$ time ./a.out 0 2281
    M2 is prime
    M3 is prime
    M5 is prime
    M7 is prime
    M13 is prime
    M17 is prime
    M19 is prime
    M31 is prime
    M61 is prime
    M89 is prime
    M107 is prime
    M127 is prime
    M521 is prime
    M607 is prime
    M1279 is prime
    M2203 is prime
    M2281 is prime

    17 prime numbers were found. It took 1.00 seconds

    real    0m1.676s
    user    0m1.652s
    sys     0m0.008s
    richard@richard-desktop:~$ time ./a.out 0 3217
    M2 is prime
    M3 is prime
    M5 is prime
    M7 is prime
    M13 is prime
    M17 is prime
    M19 is prime
    M31 is prime
    M61 is prime
    M89 is prime
    M107 is prime
    M127 is prime
    M521 is prime
    M607 is prime
    M1279 is prime
    M2203 is prime
    M2281 is prime
    M3217 is prime

    18 prime numbers were found. It took 5.00 seconds

    real    0m5.284s
    user    0m5.228s
    sys     0m0.008s
    richard@richard-desktop:~$ time ./a.out 2281 3217
    M2281 is prime
    M3217 is prime

    2 prime numbers were found. It took 4.00 seconds

    real    0m3.684s
    user    0m3.620s
    sys     0m0.016s
    richard@richard-desktop:~$ time ./a.out 21701
    M21701 is prime

    1 prime numbers were found. It took 6.00 seconds

    real    0m6.612s
    user    0m6.204s
    sys     0m0.020s