Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

timing in c++

Posted on 2003-03-09
7
Medium Priority
?
265 Views
Last Modified: 2010-04-01
if i want to time hiw much stuff it can do in 1 sec (eg. how much +1 operation in 1 sec), how do i do it?
0
Comment
Question by:madonion87
7 Comments
 

Expert Comment

by:peter_sheynkman
ID: 8097525
Use GetTickCount to measure time of any loop then calculate how much loops will happen in 1 sec

DWORD time1 = GetTickCount ();
for (int i = 0; i< NLOOPS ; i++)
{
  // do something
}
DWORD time2 = GetTickCount ();

(time2-time1)/NLOOPS will be the time of one "do something"
0
 
LVL 2

Expert Comment

by:Elias Saliba
ID: 8098031
i have some of those time functions :

----------------------------------------
#include <time.h>
#include <stdio.h>
struct tm *newtime;
time_t aclock;

void main( void )
{
   time( &aclock );                 /* Get time in seconds */

   newtime = localtime( &aclock );  /* Convert time to struct */
                                    /* tm form */

   /* Print local time as a string */
   printf( "The current date and time are: %s", asctime( newtime ) );
}
---------------------------------------
#include <time.h>
#include <stdio.h>
#include <sys/timeb.h>

struct tm *newtime;
time_t aclock;

void main( void )
{
  struct _timeb timebuffer;
   char *timeline;

   _ftime( &timebuffer );
   timeline = ctime( & ( timebuffer.time ) );

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

}
------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

void main( void )
{
   time_t   start, finish;
   long loop;
   double   result, elapsed_time;

   cout<<"Multiplying 2 floating point numbers 300 million times...\n";
   
   time( &start );
   for( loop = 0; loop < 300000000; loop++ )
      result = 3.63 * 5.27;
   time( &finish );

   elapsed_time = difftime( finish, start );
   cout<<"\nProgram takes.\n"<<elapsed_time;
}
------------------------------
any question that conserns those functions i'm ready to help ..
NOTE : all those functions where found on the msdn library

trinety
0
 
LVL 2

Expert Comment

by:Elias Saliba
ID: 8098044
>> if i want to time hiw much stuff it can do in 1 sec (eg. how much +1 operation in 1 sec), how do i do it?
- how much stuff ?: it depends on your CPU speed

and to know how much operation ur computer can do in one sec just use the LAST function i gave it to you : i.e my computer make 3.64 billion multiplication(of Real numbers) in one sec.

trinety
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 2

Expert Comment

by:Elias Saliba
ID: 8098054
ooops one more thing . just #include <iostream.h> in the last function .
trinety
0
 
LVL 5

Accepted Solution

by:
Kocil earned 60 total points
ID: 8099891
// I use the idea from Peter
// Use GetTickCount to measure the time
// CLK_TCK = ticks in 1 seconds
// However, CLK_TCK is around 18.2, so you better
// do this in 10 secs to get more accurate result
// Use clock() instead of getTickCount in Borland C++

DWORD count;
DWORD time1 = GetTickCount () + (CLK_TCK*10);
do {
  // do something
  count++;
} while (GetTickCount() < time1);

printf("You did %d operations in 1 sec\n", count / 10);
0
 

Author Comment

by:madonion87
ID: 8100978
why do you have to use dword?

anyway, i think i will accept kocil's comment
0
 

Author Comment

by:madonion87
ID: 8100980
not exactly what i wanted
but its very close and gives me an idea with only little modification to your code
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
Suggested Courses

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question