Thread: elapsed time

  1. #1
    Registered User Solarwin's Avatar
    Join Date
    Dec 2012
    Posts
    50

    elapsed time

    Hi,
    I am looking for a function or any example that shows elapsed time in seconds and minutes. I didnt find any solution for both OS Win and Linux. I am looking for example that works for both - win and linux.
    Thank You in advance!

  2. #2
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964
    You should look into the <ctime> header, it probably has what you need.
    How I need a drink, alcoholic in nature, after the heavy lectures involving quantum mechanics.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    C++11 has a chrono header, but I'll be damned if I know how to use properly. I haven't looked into it enough.
    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.

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by Elysia View Post
    C++11 has a chrono header, but I'll be damned if I know how to use properly. I haven't looked into it enough.
    Most of what it does is for measuring time spans in seconds and fractions thereof.

    for what the OP needs, it's probably perfectly adequate.

    I don't really understand why the standards committee didn't add a proper datetime class with arithmetic, comparison, and istream/ostream operators. it seems like they essentially expect us to continue to use the legacy C library functions for arithmetic, comparison, parsing and formatting operations. even if the datetime class used the legacy C stuff behind the scenes, it would still be better to have a modern, more C++-like way of handling datetime values. also fixed-point decimal types, but that's a topic for a different discussion.

  5. #5
    Registered User antred's Avatar
    Join Date
    Apr 2012
    Location
    Germany
    Posts
    257
    Probably because if they had had to reach an agreement on that too we'd be looking at C++15 instead of C++11.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Elkvis View Post
    Most of what it does is for measuring time spans in seconds and fractions thereof.

    for what the OP needs, it's probably perfectly adequate.

    I don't really understand why the standards committee didn't add a proper datetime class with arithmetic, comparison, and istream/ostream operators. it seems like they essentially expect us to continue to use the legacy C library functions for arithmetic, comparison, parsing and formatting operations. even if the datetime class used the legacy C stuff behind the scenes, it would still be better to have a modern, more C++-like way of handling datetime values. also fixed-point decimal types, but that's a topic for a different discussion.
    Yeah, I don't know either.
    Boost might be simpler, though. Here's a perfectly good example of something like the OP wants to do: Examples - 1.53.0
    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.

  7. #7
    Registered User Solarwin's Avatar
    Join Date
    Dec 2012
    Posts
    50
    I made this. What do You think guys?
    it starts counting when user enter enything.
    Code:
    #include <iostream>
    #include <ctime>
    #include <cmath>
    
    using namespace std;
    
    int main(){
    
    clock_t t;
      int f;
      t = clock();
      f = laiks (99999);
      t = clock() - t;
      cout<<"You were playing " <<((float)t)/CLOCKS_PER_SEC*100<<" seconds."<< endl;
         return 0;
    }
    int laiks (int n) {
      int i,j,freq=0;
      int sec=n-1;
      for (i=2; i<=n; ++i)
      for (j=sqrt(i);j>1;--j)
      if (i%j==0) {
      --freq;
      break;
    }
      return sec;
    }
    Last edited by Solarwin; 03-14-2013 at 11:09 AM. Reason: Elysia

  8. #8
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Why are you multiplying the time by 100?

  9. #9
    Registered User Solarwin's Avatar
    Join Date
    Dec 2012
    Posts
    50
    Quote Originally Posted by Matticus View Post
    Why are you multiplying the time by 100?
    because it showed my 0.00123 seconds. But it doesn't show me the right result anyway:/

  10. #10
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    You can multiply it by 1000 and indicate the time is in milliseconds.
    How do you know it's not the right result?

  11. #11
    Registered User Solarwin's Avatar
    Join Date
    Dec 2012
    Posts
    50
    seconds are good for me.
    I checked with stopwatch.

  12. #12
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    You must have some fast fingers

    Here's the output from my IDE console window after removing the multiplication by 100:

    Code:
    You were playing 0.232 seconds.
    
    Process returned 0 (0x0)   execution time : 0.278 s
    Press any key to continue.
    And changing the argument passed to "laiks()" from 99999 to 999999:

    Code:
    You were playing 7.312 seconds.
    
    Process returned 0 (0x0)   execution time : 7.390 s
    Press any key to continue.

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    @Solarwin:
    You know, the code is pretty unreadable. Whitespace exists for a reason. Use it! Break long lines with control statements into multiple rows!
    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.

  14. #14
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by Elysia View Post
    Boost might be simpler
    what I don't like about boost is that it doesn't have a combined datetime. it handles dates as a separate type from times. how do you do datetime arithmetic and comparisons, to second precision, without the date and time being held in the same value? boost's implementation is effectively useless to me.

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I don't know. It seems that date is a subset of time, so if you work with time, you work with dates.
    See example: Examples - 1.53.0
    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. Time elapsed
    By TitoMB345 in forum C Programming
    Replies: 13
    Last Post: 11-15-2007, 01:50 AM
  2. calculating user time and time elapsed
    By Neildadon in forum C++ Programming
    Replies: 0
    Last Post: 02-10-2003, 06:00 PM
  3. computing time elapsed?
    By redemption in forum C Programming
    Replies: 2
    Last Post: 09-09-2002, 06:55 AM
  4. Calulating Elapsed Time of any EXE
    By santhosh_be in forum C++ Programming
    Replies: 1
    Last Post: 06-03-2002, 03:59 AM