Count the actual number of operations performed during run time

J

junaidnaseer

Is it possible to actually count the number of addition or
multiplication operations performed in a program during runtime . I
know of a program that does this simply by looking through the code for
* and + operators but the problem is that this program is fooled when
we use if...else structures coz there might be a + in the if part but
the if part of the statement might not be getting actually called in a
particular execution of the program . here is a sample

if ( x==0)
y=y+1;
else
y=y+2;

now upon execution only one plus is executed but the code parsing
technique would give an answer of two which is wrong . I know there is
a way to actually count the number of operations performed during
runtime but i dont quite know how . I have to get this info to some one
else urgent so any one who knows about this plz help !
 
P

Peter Jansson

Is it possible to actually count the number of addition or
multiplication operations performed in a program during runtime . I
know of a program that does this simply by looking through the code for
* and + operators but the problem is that this program is fooled when
we use if...else structures coz there might be a + in the if part but
the if part of the statement might not be getting actually called in a
particular execution of the program . here is a sample

if ( x==0)
y=y+1;
else
y=y+2;

now upon execution only one plus is executed but the code parsing
technique would give an answer of two which is wrong . I know there is
a way to actually count the number of operations performed during
runtime but i dont quite know how . I have to get this info to some one
else urgent so any one who knows about this plz help !

How about counting all the operations by inserting code of your own?
For example, like the following;


#ifdef WE_WANT_TO_COUNT_OPS
int numberOfAdditions(0);
#endif

if ( x==0)
{
y=y+1;
#ifdef WE_WANT_TO_COUNT_OPS
numberOfAdditions++;
#endif
}
else
{
y=y+2;
#ifdef WE_WANT_TO_COUNT_OPS
numberOfAdditions++;
#endif
}


Regards,

Peter Jansson
http://www.p-jansson.com/
http://www.jansson.net/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top