Is there a timer function in C++ ?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Lana rose
    New Member
    • Nov 2006
    • 8

    #1

    Is there a timer function in C++ ?

    Hi

    I have written some code but I want to know how long it takes to run the programme. I was wondering if there is a function similar to the tic toc function in matlab that I could embed within my code.

    Your suggestions would be extremely appreciated.

    Thanks
  • horace1
    Recognized Expert Top Contributor
    • Nov 2006
    • 1510

    #2
    Originally posted by Lana rose
    Hi

    I have written some code but I want to know how long it takes to run the programme. I was wondering if there is a function similar to the tic toc function in matlab that I could embed within my code.

    Your suggestions would be extremely appreciated.

    Thanks
    if you are working a Unix environment you can use the time command to run a program and get the elapsed time, cpu time, etc. see
    http://www.mcsr.olemis s.edu/cgi-bin/man-cgi?time

    Comment

    • Lana rose
      New Member
      • Nov 2006
      • 8

      #3
      Originally posted by horace1
      if you are working a Unix environment you can use the time command to run a program and get the elapsed time, cpu time, etc. see
      http://www.mcsr.olemis s.edu/cgi-bin/man-cgi?time
      Thanks for your help but i wont be working in a Unix environment, I am running my code in windows.

      Is there a different method that I could use?

      Thanks

      Comment

      • horace1
        Recognized Expert Top Contributor
        • Nov 2006
        • 1510

        #4
        Originally posted by Lana rose
        Thanks for your help but i wont be working in a Unix environment, I am running my code in windows.

        Is there a different method that I could use?

        Thanks
        you can use function clock() in <time.h> to measure the elapsed time since program startup, e.g. to measure elapsed time for a section of code
        Code:
            time_t  start_time = clock();                              /*  get start time  */
        ........ code to measure
            float time1 = (float) (clock() - start_time) / CLOCKS_PER_SEC; 
            printf("time for code was %f seconds\n", time1);
        the actual CPU time could well be different depending upon the number of processes running, etc

        Comment

        • vpawizard
          New Member
          • Nov 2006
          • 66

          #5
          If u have VC++, u can use profile.exe. Visit Microsoft website for help and more info.

          Comment

          Working...