GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 20-Feb-2006, 11:52
dayrinni dayrinni is offline
New Member
 
Join Date: Mar 2005
Posts: 24
dayrinni is on a distinguished road

Click Ticks


EDIT: Err, CLOCK ticks, not click ticks for the subject title.

I am trying to get the amount of clock ticks that takes a certain part of my program to run.

For example:
CPP / C++ / C Code:
start = clock();
//code here
end = clock();

However! I am also using the wall clock as well as the CPU clock. So I have 2 different timers to see how my code compares. The wall clock time works fine but the clock tick code does not.
This is the code for that:
CPP / C++ / C Code:

ftime(&timebuffer);
    StartTimeW = ctime(&(timebuffer.time));
    printf( "The time is %.19s.%hu %s", StartTimeW,
	timebuffer.millitm,&StartTimeW[20] );
(along with 1 at the end of my code).

When I run the program and it displays both times (wall clock and claimed "CPU milli-second"code), interesting things happen...
They both are the same!
Shouldn't the CPU one be different, and much higher?

Anyways, here is the layout for my program:
CPP / C++ / C Code:

#include "iostream.h"
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "fstream.h"
#include "time.h"
#include <sys/types.h>
#include <sys/timeb.h>


int main (int argc, char* argv[])
{
	char* StartTimeW;
	char* EndTimeW;
	struct timeb timebuffer;
	long start;  //for start clock
	long end;   //for end clock
	
	ftime(&timebuffer);
    StartTimeW = ctime(&(timebuffer.time));
    printf( "The time is %.19s.%hu %s", StartTimeW,
	timebuffer.millitm,&StartTimeW[20] );
	
	//get the current clock time
	start = clock();

	
        //code here that we want to test timing of

	ftime(&timebuffer);
    EndTimeW = ctime(&(timebuffer.time));
    printf( "The time is %.19s.%hu %s", EndTimeW,
	timebuffer.millitm,&EndTimeW[20] );

	//get the end clock time
	end = clock();


	//display the difference and how many ticks
	cout << "CPU TIME: " << (end - start)/(double)CLOCKS_PER_SEC;
	
	return 0;

	
}

When I run the above with a small data file I have this:
Quote:
The time is Tue Feb 07 02:25:37.31 2006
The time is Tue Feb 07 02:25:37.31 2006
CPU TIME: 0

I find it hard to believe that it took the CPU 0 time to process this information even though it took less then a second.

On a larger data file:
Quote:
The time is Tue Feb 07 02:26:05.156 2006
The time is Tue Feb 07 02:26:06.406 2006
CPU TIME: 1.25

CPU and the wall clock time appear to be equal, which leads me to believe that this is incorrect. If anyone has some information regarding what is incorrect above would be good.

Thanks
  #2  
Old 20-Feb-2006, 12:50
davis
 
Posts: n/a

Re: Clock Ticks


Quote:
Originally Posted by dayrinni
I find it hard to believe that it took the CPU 0 time to process this information even though it took less then a second.

What you're looking for is a "high resolution" timer. This is a fundamental weakness of C/C++ in that in order to remain portable, the scope of resolution is limited to a "lowest-common-denominator." In C, we're at a microsecond resolution at best.

...here is a quick hack that may tell you what you want to know:

CPP / C++ / C Code:
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>

int add( const int a, const int b )
{
    return a+b;
}

void memallocfree( const int iSize )
{
    char* pData = malloc( iSize );
    if( pData )
    {
        free( pData );
        pData = 0;
    }
}

int main( int argc, char* argv[] )
{
    int i = 0;
    int b = 0;

    struct timeval tv_start;
    struct timeval tv_end;
    struct timeval tv_difference;

    gettimeofday( &tv_start, NULL );

    for( i = 0; i < 100000; i++ )
    {
        ; // just spin...
    }
    gettimeofday( &tv_end, NULL );
    timersub( &tv_end, &tv_start, &tv_difference );
    printf( "spin loop execution time: %d seconds %d microseconds\n", (int)tv_difference.tv_sec, (int)tv_difference.tv_usec );
    printf( "%.5f\n", (float)tv_difference.tv_sec + (1.0e-6 * tv_difference.tv_usec) );

    gettimeofday( &tv_start, NULL );
    for( i = 0; i < 100000; i++ )
    {
        b = add( i, i );
        // add a printf( "b = %d\n", b ); ...for much longer time!
    }
    gettimeofday( &tv_end, NULL );
    timersub( &tv_end, &tv_start, &tv_difference );
    printf( "function loop execution time: %d seconds %d microseconds\n", (int)tv_difference.tv_sec, (int)tv_difference.tv_usec );
    printf( "%.5f\n", (float)tv_difference.tv_sec + (1.0e-6 * tv_difference.tv_usec) );

    gettimeofday( &tv_start, NULL );
    for( i = 0; i < 100000; i++ )
    {
        memallocfree( 1 + i );
    }
    gettimeofday( &tv_end, NULL );
    timersub( &tv_end, &tv_start, &tv_difference );
    printf( "memallocfree loop execution time: %d seconds %d microseconds\n", (int)tv_difference.tv_sec, (int)tv_difference.tv_usec );
    printf( "%.5f\n", (float)tv_difference.tv_sec + (1.0e-6 * tv_difference.tv_usec) );

    return 0;
}

// output:

spin loop execution time: 0 seconds 132 microseconds
0.00013
function loop execution time: 0 seconds 988 microseconds
0.00099
memallocfree loop execution time: 0 seconds 10307 microseconds
0.01031


:davis:
 
 

Recent GIDBlogOnce again, no time for hobbies by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Ads - - - what if I click on my website?? wish71 AdSense Forum 11 03-Jul-2007 21:59
Re: AdSense, now you can click them on your site JdS AdSense Forum 12 19-Feb-2006 16:11
how click in my ads saracool AdSense Forum 4 19-Feb-2006 14:45
Sydog's DVD to Divx Conversion Guide asanthadenz Computer Software Forum - Windows 1 14-Mar-2003 15:08

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 19:53.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.