Post your speeds - Calculate 50 Million Factorials

Posted:
in Current Mac Hardware edited January 2014
Here are single-threaded and multithreaded versions of a simple C app I threw together last night that calculates a factorial 50 million times.



Right-click and save link to disk for both, then compile and run from the Terminal or paste into new projects in Project Builder. (To just read the code, left-click on the links)



My dual- 1 gHz G4 tower gets about 25 seconds with default gcc compiler settings and 12 seconds with compiler optimization set to the "-O3" flag.



Since this app uses 64-bit arithmetic, anyone with a G5 please let us see what kinds of times you are getting.



http://www.johnnylundy.com/unthreaded_factorial.c Single-Threaded; Right-Click and Download Link To Disk



http://www.johnnylundy.com/threaded_factorial.c Multi-Threaded; Right-Click and Download Link To Disk
«1345678

Comments

  • Reply 1 of 146
    You've got some problems with your code:



    THREAD_COUNT should be 1, not 2, for the single-threaded version. Also, threads[i].numLoops should be set equal to ITERATIONS/THREAD_COUNT and not ITERATIONS/2.



    With these changes:



    Dual 2.8 GHz Xeon running Red Hat Linux 7.2

    single-threaded 20 sec.

    double-threaded 10 sec.





    800 MHz Powerbook G4 (DVI)

    single-threaded 28 sec.





    AMD Athlon 1.4 GHz (Thunderbird)

    single-threaded 21 sec.



    -- Mark
  • Reply 2 of 146
    lundylundy Posts: 4,466member
    Quote:

    Originally posted by mark_wilkins

    You've got some problems with your code:



    THREAD_COUNT should be 1, not 2, for the single-threaded version. Also, threads[i].numLoops should be set equal to ITERATIONS/THREAD_COUNT and not ITERATIONS/2.





    -- Mark




    Thanks for pointing that out - I uploaded the same file twice I think.



    I'll see if I accidently overwrote the original.



    I also found that running RC5 takes 40% of the processor(s) even though it is supposed to be zero-priority.





    All G5 owners please feel free to participate.



    Thanks...



    Johnny



    EDIT: Unthreaded version now uploaded properly. Link fixed.
  • Reply 3 of 146
    lucaluca Posts: 3,833member
    How do you run this in Terminal? I remember running a Perl program once, I just typed "perl (name of program)" and it ran. But I don't know what the command is for compiling C.
  • Reply 4 of 146
    Quote:

    Originally posted by Luca Rescigno

    How do you run this in Terminal? I remember running a Perl program once, I just typed "perl (name of program)" and it ran. But I don't know what the command is for compiling C.



    Multi-proc:



    Code:




    cc -O3 -lpthread -o threaded_factorial threaded_factorial.c











    One proc:



    Code:




    cc -O3 -lpthread -o unthreaded_factorial unthreaded_factorial.c









    -- Mark
  • Reply 5 of 146
    400 Mhz iMac DV (Grape)

    Mac OS X 10.2.6

    GCC version 1161, based on gcc version 3.1 20020420 (prerelease)



    % gcc -O3 -o unthreaded_factorial unthreaded_factorial.c

    % ./unthreaded_factorial

    Start: 1063076495 End: 1063076565



    i= 50000001

    Time=70



    % gcc -O3 -o threaded_factorial threaded_factorial.c

    % ./threaded_factorial

    Creating Thread Number: 0

    Creating Thread Number: 1

    Loop Done; Time=71 secs for thread#:0, Loops=25000000

    Loop Done; Time=71 secs for thread#:1, Loops=25000000
  • Reply 6 of 146
    lucaluca Posts: 3,833member
    It didn't work. Says "cc: command not found"
  • Reply 7 of 146
    Quote:

    Originally posted by Luca Rescigno

    It didn't work. Says "cc: command not found"



    You have to install the contents of the optional Developer Tools disk that came with Mac OS X (or your computer, if it came with OS X preinstalled.)



    -- Mark
  • Reply 8 of 146
    lucaluca Posts: 3,833member
    Ah, well mine probably didn't come with DevTools installed. But I don't care enough to install them so I'll just leave it be
  • Reply 9 of 146
    eugeneeugene Posts: 8,254member
    2x1000 MHz QuickSilver G4. Used Apple GCC 3.3 (build 1409) and IBM XLC beta.



    Code:


    > cc -O3 -o unthreaded_factorial unthreaded_factorial.c

    > ./unthreaded_factorial

    Start: 1063079616 End: 1063079637



    i= 50000001

    Time=21





    Code:


    > cc -O3 -o threaded_factorial threaded_factorial.c

    > ./threaded_factorial

    Creating Thread Number: 0

    Creating Thread Number: 1

    Loop Done; Time=11 secs for thread#:0, Loops=25000000

    Loop Done; Time=11 secs for thread#:1, Loops=25000000





    Code:


    > xlc -O5 -o unthreaded_factorial unthreaded_factorial.c

    > ./unthreaded_factorial

    Start: 1063079525 End: 1063079542



    i= 50000001

    Time=17





    Code:


    > /opt/ibmcmp/vac/6.0/bin/xlc -O5 -o threaded_factorial threaded_factorial.c

    > ./threaded_factorial

    Creating Thread Number: 0

    Creating Thread Number: 1

    Loop Done; Time=9 secs for thread#:1, Loops=25000000

    Loop Done; Time=9 secs for thread#:0, Loops=25000000



  • Reply 10 of 146
    Dual 800 G4, 768MB RAM, gcc 3.1



    Multithreaded:

    Loop Done; Time=14 secs for thread#:1, Loops=25000000

    Loop Done; Time=14 secs for thread#:0, Loops=25000000



    Single threaded:

    Time=27



    14 and 27 seconds, not bad at all considering the age of my machine and the results of the competition above.
  • Reply 11 of 146
    gabidgabid Posts: 477member
    Quote:

    Originally posted by Luca Rescigno

    Ah, well mine probably didn't come with DevTools installed. But I don't care enough to install them so I'll just leave it be



    I'm in the same boat: I'm curious to see results from my machine, but I have no real need to install the Dev Tools. Though if anyone could get this running as some type of little app, I have a 1.8 GHz G5 all ready to be tested...
  • Reply 12 of 146
    lundylundy Posts: 4,466member
    Quote:

    Originally posted by Gabid

    I'm in the same boat: I'm curious to see results from my machine, but I have no real need to install the Dev Tools. Though if anyone could gte this running as some type of little app, I have a 1.8 GHz G5 all ready to be tested...



    I'll put up a binary - I don't recall the G5-specific compiler flags though.



    Anybody remember what those are?



    EDIT: I think this zipped binary should download OK:





    Right-click, save and unzip
  • Reply 13 of 146
    gabidgabid Posts: 477member
    Quote:

    Originally posted by lundy

    I'll put up a binary - I don't recall the G5-specific compiler flags though.



    Anybody remember what those are?



    EDIT: I think this zipped binary should download OK:





    Right-click, save and unzip




    Ok. Got it. Now what? Unfortunately my knowledge of the Terminal stops at the "cd" command...and I think I can open things as well
  • Reply 14 of 146
    qaziiqazii Posts: 305member
    Sun Fire 880: 4x Sun UltraSPARC-III @ 750Mhz:

    Code:


    ./unthreaded_factorial

    Start: 1063087627 End: 1063087840



    i= 50000001

    Time=213











    Code:


    ./threaded_factorial

    Creating Thread Number: 0

    Creating Thread Number: 1

    Loop Done; Time=107 secs for thread#:0, Loops=25000000

    Loop Done; Time=103 secs for thread#:1, Loops=25000000









    Oddly, top showed only one processor being used at once. The two threads did not end anywhere close to the same time.



    Can we have a 4-proc version?
  • Reply 15 of 146
    Dual 533:

    Code:


    Creating Thread Number: 0

    Creating Thread Number: 1

    Loop Done; Time=29 secs for thread#:1, Loops=25000000

    Loop Done; Time=29 secs for thread#:0, Loops=25000000





  • Reply 16 of 146
    lundylundy Posts: 4,466member
    Quote:

    Originally posted by qazII

    Sun Fire 880: 4x Sun UltraSPARC-III @ 750Mhz:



    Oddly, top showed only one processor being used at once. The two threads did not end anywhere close to the same time.



    Can we have a 4-proc version?




    The output shows they were a few seconds apart. You mean the threads appeared to run sequentially? That could be - nondetached is assumed by this code, and isn't necessarily true for every C implementation.



    To enable for 4 processors, find this code in the source:





    #define THREAD_COUNT 2 //Two processors

    #define ITERATIONS 50000000 //Change this to however many loops desired



    ThreadInfo threads[THREAD_COUNT]; //Array of 2 thread info structures

    int result, i;



    // initialize the Thread Info for threads 0 and 1:

    for (i = 0; i < THREAD_COUNT; i++) {

    threads[i].index = i; //Set thread ID

    threads[i].numLoops = ITERATIONS/2; //Divide by # CPUs





    Change the 2 to a 4 in the first line

    Change the "ITERATIONS/2" to "ITERATIONS/THREAD_COUNT" in the last line
  • Reply 17 of 146
    lundylundy Posts: 4,466member
    Quote:

    Originally posted by Gabid

    Ok. Got it. Now what? Unfortunately my knowledge of the Terminal stops at the "cd" command...and I think I can open things as well



    Sorry. cd to the directory that the unzipped file is in, and type



    ./unthreaded_factorial



    That should execute the binary, hopefully is not corrupted.
  • Reply 18 of 146
    17" iMac 1Ghz, 1Gig RAM



    Code:




    paul% gcc -O3 -lpthread -o threaded_factorial threaded_factorial.c

    paul% ./threaded_factorial

    Creating Thread Number: 0

    Creating Thread Number: 1

    Loop Done; Time=22 secs for thread#:1, Loops=25000000

    Loop Done; Time=22 secs for thread#:0, Loops=25000000



    paul% gcc -O3 -lpthread -o unthreaded unthreaded_factorial.c

    paul% ./unthreaded

    Start: 1063092344 End: 1063092367



    i= 50000001

    Time=23



    paul% gcc -o unthreaded unthreaded_factorial.c[82-42-73-213:~/

    paul% ./unthreaded



    Start: 1063092384 End: 1063092430



    i= 50000001

    Time=46





  • Reply 19 of 146
    eugeneeugene Posts: 8,254member
    Quote:

    Originally posted by lundy

    I'll put up a binary - I don't recall the G5-specific compiler flags though.



    Anybody remember what those are?




    For GCC 3.3:

    -mcpu=970

    -mtune=970

    -mpowerpc64



    That's a good place to start.
  • Reply 20 of 146
    pbpb Posts: 4,255member
    Quote:

    Originally posted by lundy

    Here are single-threaded and multithreaded versions of a simple C app I threw together last night that calculates a factorial 50 million times.





    This is an interesting test. It would be, however, more interesting to see how the relative rating, between the several configurations, changes when you make your code a little more complicated. For example, how about calculating the factorials for all integers between 1 and, say, 20000?
Sign In or Register to comment.