Thread: Is there a standard function?

  1. #91
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Quote Originally Posted by cyberfish View Post
    Core 2 Duo at 3GHz, 64-bit Linux, GCC 4.4.1.

    Un-optimized version runs in 150 seconds, so your compiler is most certainly optimizing, just not nearly as much.
    .
    Did you make the fixes to the code in order to get rid of the compiler warnings, or did you run it 'as is'?

  2. #92
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Passing the length of the number to the function as an argument allowed the compiler to inline it again. The new optimization to remove log10 shaved off a second off that time (9 seconds now).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #93
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    I only got warnings for using %d for time_t, so I ran it as is.

  4. #94
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    I'm not sure about MSVC, but gcc has a compiler flag that sets inline aggressiveness.

  5. #95
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yes, there is __forceinline to try to force it to inline. I've been successful so far to make it inline. Now trying a division table.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #96
    Registered User
    Join Date
    Jun 2010
    Posts
    182
    Quote Originally Posted by cyberfish View Post
    Core 2 Duo at 3GHz, 64-bit Linux, GCC 4.4.1.

    Un-optimized version runs in 150 seconds, so your compiler is most certainly optimizing, just not nearly as much.


    Yeah I was talking to frktons.
    Well, mine is 2.4 Ghz, about 20% slower, and now I got curious about the
    difference GCC and 64 bit mode are doing. As I have time enough I'll give
    them a look.

  7. #97
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Funny how my test all hate lookup tables. They always turn out slower.
    Both in filling and accessing. It seems it's faster to do the division twice.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #98
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    64 bit doesn't make any difference. I got similar results in 32 bit. It's the compiler.

  9. #99
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Compilers are picky things. I know I got it down to around 9 seconds, but I cannot seem to reproduce it now.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #100
    Registered User
    Join Date
    Jun 2010
    Posts
    182
    Quote Originally Posted by Elysia View Post
    Compilers are picky things. I know I got it down to around 9 seconds, but I cannot seem to reproduce it now.
    What HW/SW are you using? Win7 64 bit/MSV2010 and what else?
    What are your timings with the last code I posted ?

  11. #101
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    AMD Athlon II X2 250 (3 GHz).
    I will try yours shortly.

    EDIT: Your code: 39 seconds.
    EDIT2: My code with your integer: 29 seconds.

    Code:
     	Address     	Line 	Source                     	Code Bytes 	Timer samples 	
     	0x13f3f11e1 	181  	        remain = num % 10; 	           	48.99
    o_O
    Last edited by Elysia; 07-04-2010 at 02:45 PM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #102
    Registered User
    Join Date
    Jun 2010
    Posts
    182
    Quote Originally Posted by Elysia View Post
    AMD Athlon II X2 250 (3 GHz).
    I will try yours shortly.

    EDIT: Your code: 39 seconds.
    EDIT2: My code with your integer: 29 seconds.

    Code:
     	Address     	Line 	Source                     	Code Bytes 	Timer samples 	
     	0x13f3f11e1 	181  	        remain = num % 10; 	           	48.99
    o_O
    OK. Thanks Elysia. Enough for today. As time permits I'll be back with new
    ideas. Have a nice time. :-)

  13. #103
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    One optimization I would do is to precompute a mapping of integers 0-999 to their 3 characters strings.

    That would take 1000*3 = ~3KB of memory. No null terminators needed because you know they are exactly 3 characters long.

    Then you can do % 1000 instead of % 10. I'm guessing that will make it at least twice as fast.

    It will also make the logic simpler (since you want a comma every 3 digits), and eliminate a bunch of branching, which are also slow with modern processors.

    And switching to a modern compiler will make this all not matter .

  14. #104
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Branching? Slow? I don't buy that argument so much.
    Today's processors are pretty clever, and since the loop is pretty deterministic, it should easily be able to avoid branch misprediction.
    I'm just speculating, though.

    But that idea would probably see some speed gains.
    I'm going to try it.

    EDIT: 14 seconds vs 29 on x64.
    EDIT2: With a slightly larger table and x64, it is possible to reduce the runtime to ~8 seconds (if you are willing to sacrifice ~8 MB memory).
    The only problem is that you have to correct the numbers:

    Code:
     The value of num is: -1234567890
    
     init_time = 1278283073
    
     The formatted value of num is: -001,234,567,890
    
     end_time  = 1278283081
    
    
     The routine test has taken about 8 seconds
    
     to perform +000,500,000,000 cycles of the formatting function
    Last edited by Elysia; 07-04-2010 at 04:48 PM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #105
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    Hmm when you go to 8MB of memory, I don't see how it can be faster.

    8MB is larger than the L2 of most/all CPUs, and the values are pretty much random. Cache misses should be much more expensive than doing the calculations.

    The loop is fine. The compiler will probably at least partially unroll it anyways.

    I was talking about this one
    Code:
    if (count == 3)
    It's true once every 3 iterations in each function call (not globally). I'm not sure if the branch predictor is that smart.

    But the biggest difference would be the elimination of division (%).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Change this program so it uses function??
    By stormfront in forum C Programming
    Replies: 8
    Last Post: 11-01-2005, 08:55 AM
  3. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM
  4. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM
  5. Replies: 5
    Last Post: 02-08-2003, 07:42 PM