Thread: What is the unit?

  1. #1
    Registered User
    Join Date
    Mar 2017
    Posts
    1

    What is the unit?

    Hi members,

    I wrote a code that calculates the execution time of the for loop.

    Code:
    #include <stdio.h>
    #include <time.h>
    
    int main( )
    {
        clock_t start, stop;
        int x;
        double duration;
    
        start = clock();  // get number of ticks before loop
    
        for( x = 0; x < 10; x++ ){
          printf("Hello");
           }
    
        stop = clock();  // get number of ticks after loop
    
        // calculate time taken for loop
        duration = ( double ) ( stop - start ) / CLOCKS_PER_SEC;
    
        printf( "Time taken is: %f\n", duration );
    
        return 0;
    }
    What is the unit of the value that is derived in the variable 'duration'? Is it in seconds or milliseconds?
    Last edited by windprorobin; 03-09-2017 at 10:31 PM.

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Think of it this way:
    start and stop are in "clock" units.
    CLOCKS_PER_SEC can be literally rewritten as "clock/s"

    and if you combine them:
    clock/(clock/s) = s*clock/clock = s

    Therefore, "duration" is in seconds.
    Devoted my life to programming...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Unit tests
    By Aslaville in forum C++ Programming
    Replies: 3
    Last Post: 01-10-2017, 08:03 AM
  2. Unit Converter
    By rohitdwivedula in forum C++ Programming
    Replies: 1
    Last Post: 02-08-2014, 06:00 PM
  3. C++ Unit Tests
    By nelson777 in forum C++ Programming
    Replies: 2
    Last Post: 03-29-2011, 07:16 PM
  4. Unit test for c++
    By rluceac in forum C++ Programming
    Replies: 5
    Last Post: 04-16-2009, 02:24 AM
  5. Unit Actions
    By DavidP in forum Game Programming
    Replies: 20
    Last Post: 05-28-2004, 09:18 PM

Tags for this Thread