?
Solved

timing in c++

Posted on 2003-03-09
7
Medium Priority
?
256 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
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 goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
Suggested Courses

762 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