2nd Attempt : How can I count the actual number of operations performed in a program

J

junaidnaseer

ok I know I posted this question previously and then I got a reply and
I realized that I had asked a really dumb question but now I realize
that my question wasn't that dumb at all !

I had asked that I wanted to find the number of operations ( like
additions ,subtractions ,shifts,comparisons ,etc ) performed in a
program in runtime . Now one way would be to read the .c or .cpp file
and count the number of ' + ' or ' << ' or whatever . But this way the
program miscounts ! why ? look below

if ( x==0)
y+y;
else
z+z;

now the program would count two addition operations when only one is
performed .

I was informed by a very nice gentleman from sweden I think that I
could do it like this :
int no_of_add = 0;
if ( x==0 )
y+y;
no_of_add++;
else
z+z;
no_of_add++ ;

this works fine but I have a problem ! My program is 1600 LINES LONG
and its just one FILE in 32 OTHER FILES . I would probably have to
write the entire thing again !

Another option that was brought up by someone was OPERATOR OVERLOADING
.. Well you can forget about that because my program is not written in
classes and OPERATOR OVERLOADING works only for OBJECT Type data , it
will not work on INT or FLOAT . And I can't write the entire program
again because of its length . :C

Can anyone help :-*
 
P

Phlip

I had asked that I wanted to find the number of operations ( like
additions ,subtractions ,shifts,comparisons ,etc ) performed in a
program in runtime .

What will you do with the number?
 
B

BigBrian

if ( x==0)
y+y;
else
z+z;

now the program would count two addition operations when only one is
performed .

I was informed by a very nice gentleman from sweden I think that I
could do it like this :
int no_of_add = 0;
if ( x==0 )
y+y;
no_of_add++;
else
z+z;
no_of_add++ ;

The above code will not compile. It will give an error like

error: expected `}' before 'else'

This is exactly why you should always use "{" and "}"
 
M

mlimber

Now one way would be to read the .c or .cpp file
and count the number of ' + ' or ' << ' or whatever . But this way the
program miscounts ! why ? look below

if ( x==0)
y+y;
else
z+z;

now the program would count two addition operations when only one is
performed .
[snip]

Why would you write code like this if you are not using overloaded
operators? What does it do? (The only thing I can think of is that x,
y, and/or z are volatile and you want to force two successive reads,
but there are certainly clearer ways to do that!) One might consider
such code as inherently buggy because the intent is not clear.

Moreover, your compiler has considerable liberty to rearrange or delete
expressions, so long as doing so would not change the result of the
program. Shouldn't you be counting on the compiled code? And more to
the point, shouldn't you be using some existing profiling tool to make
your measurements? It seems very likely that you are barking up the
wrong tree altogether.

Cheers! --M
 
P

Puppet_Sock

if ( x==0)
y+y;
else
z+z; [snip]
if ( x==0 )
y+y;
no_of_add++;
else
z+z;
no_of_add++ ;

Well, apart from the syntax problems. And the other problems
mentioned with what you are doing. Such as if y is a built in
such as an int, then the statement

y+y;

seems pretty pointless in most circumstances.

It looks like you are trying to get performance information out
of your code. I'm guessing (and it's a wild stab in the very
dimly lit room) that you want to know thing like how long it
takes to do certain functions, how to compare the CPU
absorbed by various activities, where you app is spending
most of its time, where it burns most CPU, where it spends
time waiting for the disk I/O to finish, etc. Such information
will give you some help at tweaking an app for performance
by telling you what parts potentially have large savings.
If a part of your code represents only 1 percent of the runtime,
you may decide not to bother doing any serious optimization
even if it represents the bulk of source lines. On the other
hand, if a small section of code reprsented 90 percent of
the run time, it might be very valuable to optimize it very
carefully. Note I said "might."

If so, what you are looking for is called "instrumentation."
There are various products on the market that take your
program and add lines of code along the lines you have
done, but with correct syntax. And they keep track of
various performance issues as mentioned, and produce
reports after test runs.

If that's really what you are looking for (and it's not very
much like what you asked) then you should go do a
Google search for "CASE tools" and see what you get.
(CASE stands for "computer aided software enginnering.")
Socks
 
T

Tomás

posted:
ok I know I posted this question previously and then I got a reply and
I realized that I had asked a really dumb question but now I realize
that my question wasn't that dumb at all !


Here's the route I'd take. (And yes it's dirty). Here comes some unchecked,
off-the-cuff code:

class Counter {

static unsigned long total_amount; // <- Total amount for all types
unsigned long specific_amount; // <- Total amount for a specific type

public:

Counter() : specific_amount(0) {}

Counter& operator++() { ++total_amount; ++specific_amount; return *this;
}

static unsigned long GetTotalAmount() { return total_amount; }

unsigned long GetAmount() const { return specific_amount; }
};


unsigned long Counter::total_amount = 0;


template<class T>
class PrimitiveTypeHolder {
private:

T value;

public:

static Counter counter;

T& operator+=(const T& rhs)
{
++counter; /* Record the operation */

value += rhs;

return *this;
}

T operator+(const T& rhs)
{
return PrimitiveTypeHolder<T>(*this).operator+=(rhs);
}

};


Then, I would use macro trickery to turn:

int a;
float b;
double c;


into:

PrimitiveTypeHolder<int> a;
PrimitiveTypeHolder<float> b;
PrimitiveTypeHolder<double> c;


I'm not sure if the following macro would work:

#define int PrimitiveTypeHolder<int>


And then at the end of your program, you can check the total amount of
operations:

cout << Counter::GetTotalAmount();


Or just for a given type:

cout << PrimitiveTypeHolder<float>().counter.GetAmount();


There's probably a thousand ways of doing this, but this one came to mind.


-Tomás
 
J

junaidnaseer

@Philip

Someone just gave me a task and I need help I don't know what that
person will do with this number , maybe he wants to check the program
efficiency or something !

and by the way the link does not work

@Brian @ Mlimber @ PuppetSock
The code I gave was just a sample code just to illustrate my problem .
It's not the acutal code .I know that code is not correct and it makes
no sense . Try to grasp the problem and not the code or my neck for
that matter . I know this has been done but I don't know how .

@ Mlimber
Shouldn't you be counting on the compiled code? And more to
the point, shouldn't you be using some existing profiling tool to make
your measurements?

The problem is to count operations on runtime , not on compilation or
after linking !
If some profiler or debugger can do this then please guide this poor
soul .

@ Puppet_Sock

Thank you for guiding me ! That's exactly what I wanted to do ! Thanks
a lot ! I tried to be as clear as possible , sorry if could not be more
elaborative !

@Tomas

Thank you too ! I will try that code ! Thanks for helping out
 
M

mlimber

junaidnaseer said:
@ Mlimber

the point, shouldn't you be using some existing profiling tool to make
your measurements?

The problem is to count operations on runtime , not on compilation or
after linking !
If some profiler or debugger can do this then please guide this poor
soul .

My point was that you don't know what the compiler is doing to your
code. It might optimize some operation that you *think* is expensive.
For instance, on one processor I have worked on, the pipeline has to be
flushed for a branch instruction, which meant 5 wasted cycles, but
sometimes, the compiler (with no optimization flags enabled) would
squeeze in some other operation in those cycles that didn't interfere
with the branch. Thus, that operation was effectively done in parallel
with other tasks. Also, the same processor (a TI DSP, if you care) had
several multipliers on it, so a statement like:

x = (a*b) + (c*d) + (e*f) + (g*h);

could be executed largely in parallel. Also consider that the compiler
might inline certain functions, optimize away certain operations, and
do other things that could throw off your calculations. Add to that
cache and locality of reference issues, and you've pretty much
invalidated most simplistic operation counting schemes.

Anyway, the issue is that your guess about how fast the code is based
on the number of additions and so forth is not necessarily (or even
likely to be) an accurate measurement of how the *compiled* code
actually performs.

If your system didn't come with a profiler, use Google to find one for
your system. It will help you *accurately* estimate the speed and find
what portions need optimized. Certain products might also give you
statistics on certain types of operations.

Cheers! --M
 
J

junaidnaseer

Hi Again !
First of all I want to thank everyone who took time out just to help
me . Now here is my problem ( I will try to put it as clearly as
possible ) .
I have a 32 file project that has one particular file that I want to
analyze . That file is around 1536 lines long . Now which tool would
there be ( if any ) that can analyze the file or the project as a whole
and give me the actual number of additions performed during runtime .
Please name any particular profiler or CASE tool that can do this . I
am working on Visual C++ 6.0 . Remember that the program is modular
i.e. it's a multi-file program ( if that makes any difference ). Name
both commercial products as well as free softwares ( if any ) that can
do this . This might not be the correct place to ask these questions
but you people are my only hope .And one thing more I have worked
extensively on C++ but I have never used any debugger , profiler CASE
tool ,etc. ( Never needed them uptill now ! )
Thanks a lot everyone for bearing with me uptill now !

Junaid Naseer
 
J

junaidnaseer

Hi Again !
First of all I want to thank everyone who took time out just to help
me . Now here is my problem ( I will try to put it as clearly as
possible ) .
I have a 32 file project that has one particular file that I want to
analyze . That file is around 1536 lines long . Now which tool would
there be ( if any ) that can analyze the file or the project as a whole
and give me the actual number of additions performed during runtime .
Please name any particular profiler or CASE tool that can do this . I
am working on Visual C++ 6.0 . Remember that the program is modular
i.e. it's a multi-file program ( if that makes any difference ). Name
both commercial products as well as free softwares ( if any ) that can
do this . This might not be the correct place to ask these questions
but you people are my only hope .And one thing more I have worked
extensively on C++ but I have never used any debugger , profiler CASE
tool ,etc. ( Never needed them uptill now ! )
Thanks a lot everyone for bearing with me uptill now !

Junaid Naseer
 

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

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top