Thread: claculate time of function

  1. #16
    Registered User zedd's Avatar
    Join Date
    Jun 2015
    Posts
    14
    Only one comment: I think that to perform correctly the calculation of the elapsed time, the frequency scaling of the CPU must be disabled.

  2. #17
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    No it doesn't. Modern operating system will handle that stuff for you. But it should be disabled for precise measurements since frequency scaling introduces more uncertainty.
    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. #18
    Registered User
    Join Date
    Apr 2012
    Posts
    87
    Quote Originally Posted by zedd View Post
    Only one comment: I think that to perform correctly the calculation of the elapsed time, the frequency scaling of the CPU must be disabled.
    Thanks for the reply, but how we disable this frequency ?

  4. #19
    Registered User
    Join Date
    Apr 2012
    Posts
    87
    Hi,
    i used the clock_t to calculate the run time of another function
    i get "0 seconde"?
    i say maybe it's in millysecond
    i used
    Code:
     cout << (double(clock() - timestart) / CLOCKS_PER_SEC) * 1000<< " seconds." << endl;
    i get as the first one '0 seconde"
    ?
    Note: there is many simple instruction , as sqrt, min, ....

  5. #20
    Registered User
    Join Date
    Apr 2012
    Posts
    87
    I used this
    Code:
     
    clock_t start, stop;
     
     
        start = clock();
    //
    // block of instruction that i want calculate it's running  time
    //
    stop = clock();
     
        printf("Approx seconds, tenths, hundredths and milliseconds: %.3f\n", 
            ((double)(stop - start) / CLOCKS_PER_SEC));
    I get 0.000 i will be crazy how many instructions give me 0 sec? it's impossible no?

  6. #21
    Registered User zedd's Avatar
    Join Date
    Jun 2015
    Posts
    14
    [QUOTE]Thanks for the reply, but how we disable this frequency ?[/QUOTE]

    Three possibilities:
    - some BIOS usually permits this configuration
    - Linux provides a specific widget
    - the easiest way is to perform a number of operations that load the CPU before the timing of the function

  7. #22
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by dreamvig View Post
    Thanks for the reply, but how we disable this frequency ?
    In Windows, you can go to power management and set the minimum and maximum cpu frequency to the same value. You can also use the high performance power plan to stop it from using power saving features. I'm not sure how it affects TurboBoost, though. But you can set the min and max to 99% to disable that, I'm pretty sure.

    Quote Originally Posted by dreamvig View Post
    I get 0.000 i will be crazy how many instructions give me 0 sec? it's impossible no?
    Not really. I think you underestimate how fast computers can be. Throw in a few loops and take the average.

    Quote Originally Posted by zedd View Post
    - the easiest way is to perform a number of operations that load the CPU before the timing of the function
    This does not disable power savings. It is a pretty poor way.
    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. #23
    Registered User
    Join Date
    Apr 2012
    Posts
    87
    Not really. I think you underestimate how fast computers can be. Throw in a few loops and take the average.
    but i what i need is just the run time of my function , if i get 0 many time
    i can't write it

  9. #24
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Repeat what you want to time several times, then take the average. That way you get an approximation of the "real" time.
    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. #25
    Registered User
    Join Date
    Apr 2012
    Posts
    87
    Repeat what you want to time several times, then take the average. That way you get an approximation of the "real" time.
    what you mean here ?
    did you mean i run my app several time ? because i made that and always i get "0.000"

  11. #26
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
    	namespace stdc = std::chrono;
    	auto start = stdc::high_resolution_clock::now();
    	constexpr int LoopCount = 1000000;
    	for (int i = 0; i < LoopCount; i++)
    	{
    		// My code here
    	}
    	auto end = stdc::high_resolution_clock::now();
    	std::cout << stdc::duration_cast<stdc::milliseconds>((end - start) / LoopCount).count() << "\n";
    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. #27
    Registered User
    Join Date
    Apr 2012
    Posts
    87
    Quote Originally Posted by Elysia View Post
    Code:
        namespace stdc = std::chrono;
        auto start = stdc::high_resolution_clock::now();
        constexpr int LoopCount = 1000000;
        for (int i = 0; i < LoopCount; i++)
        {
            // My code here
        }
        auto end = stdc::high_resolution_clock::now();
        std::cout << stdc::duration_cast<stdc::milliseconds>((end - start) / LoopCount).count() << "\n";
    With this code i can get the "real" time?
    can you please explain to me why without loop it does not work, and why i must use loop and excute many time the same code

  13. #28
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Because without the loop, it may be too fast. Generally, execution time under a millisecond contains too much "noise." So when you rerun the code many times, you will see a lot of variation. The best way to "approximate" the real run time is to execute it multiple times and take the average/mean. You should also change the loop count to match your code. Too few iterations will mean that you won't get a good average or you will get 0, and too many and it will take too long to execute.

    Refer to my last post on the last page for more details.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 04-17-2013, 11:32 PM
  2. Replies: 2
    Last Post: 04-17-2013, 12:25 AM
  3. Replies: 2
    Last Post: 01-12-2013, 10:11 AM
  4. Time function in C
    By Bugs Bunny in forum C Programming
    Replies: 8
    Last Post: 11-18-2007, 11:25 AM
  5. Need Help with the time()....Function.
    By Patrick1234 in forum C++ Programming
    Replies: 1
    Last Post: 11-08-2002, 02:14 PM