timing in c++

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?
madonion87Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

peter_sheynkmanCommented:
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
Elias SalibaCommented:
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
Elias SalibaCommented:
>> 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
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Elias SalibaCommented:
ooops one more thing . just #include <iostream.h> in the last function .
trinety
0
KocilCommented:
// 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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
madonion87Author Commented:
why do you have to use dword?

anyway, i think i will accept kocil's comment
0
madonion87Author Commented:
not exactly what i wanted
but its very close and gives me an idea with only little modification to your code
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.