Thread: Beginner level issue: next prime number

  1. #16
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Quote Originally Posted by flp1969 View Post
    Code:
    $ gcc -O2 -ffast-math -o sqrt-test sqrt-test.c
    $ ./sqrt-test 1000000000
     sqrt( 1000000000 ) = 31622 (109 cycles)
    isqrt( 1000000000 ) = 31622 (553 cycles)
    So, without the proper optimizations, isqrt() is, indeed, faster.
    Very interesting. I compiled without -ffast-math and I tested using no optimisations, -O2 and -O3. And for each option and 1073741822 iterations and timing using the time command (time ./a.out) and valgrind both showed sqrt() to be significantly faster on my i7-8700K. Just one of those things I guess.

  2. #17
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Quote Originally Posted by flp1969 View Post
    So, without the proper optimizations, isqrt() is, indeed, faster.
    Ok, I was bored so I decided to repeat my tests.

    For 1 call of each function (sqrt() and isqrt()), isqrt() is indeed faster than calling sqrt() (about twice as fast on my CPU and according to valgrind). So, you're correct.

    The big difference in speed happens when I make hundreds of thousands of calls to each (this is when, on my computer, the sqrt() method is significantly faster overall -- for all the calculations). I'm testing in a loop and the calls are not being optimised away (I have the variables as volatile and also checked the asm), so that doesn't explain it. I /am/ however calculating, in the loop, the square root of consecutive numbers (for (i = 0; i < ITERATIONS; i++)). I assume that because of this oversight on my part (using consecutive parameters) that the CPU is caching... something... explaining the huge discrepancy. I could probably try calculating the square root of a random number for each iteration and perhaps that would give different results but is it worth it?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help needed for a programme .. (beginner level )
    By dineshpabbi10 in forum C Programming
    Replies: 4
    Last Post: 06-04-2015, 10:48 PM
  2. Replies: 3
    Last Post: 12-02-2014, 10:11 AM
  3. Help! beginner level question
    By seking10788 in forum C Programming
    Replies: 6
    Last Post: 07-07-2011, 08:15 AM
  4. Can someone help me with this beginner-level program?
    By matthayzon89 in forum C Programming
    Replies: 5
    Last Post: 05-09-2010, 11:00 AM
  5. Replies: 3
    Last Post: 03-29-2005, 04:24 PM

Tags for this Thread