473,394 Members | 1,812 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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

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 :-*

May 8 '06 #1
9 2396
ju**********@gmail.com wrote:
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?

--
Phlip
http://www.greencheese.us/ZeekLand <-- NOT a blog!!!
May 8 '06 #2
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 "}"

May 8 '06 #3
ju**********@gmail.com wrote:
[snip]
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

May 8 '06 #4
ju**********@gmail.com wrote:
[snip]
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

May 8 '06 #5
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
May 8 '06 #6
@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

May 10 '06 #7
junaidnaseer wrote:
@ 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 .


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

May 10 '06 #8
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

mlimber wrote:
junaidnaseer wrote:
@ 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 .


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


May 10 '06 #9
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

May 10 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Arsen V. | last post by:
Hello, What is the best way to accomplish the following. 1) An ASP.NET program (consiting of one file somepage.aspx) receives about 25,000,000 requests pay day. 2) The requests come from a...
4
by: Matthew Groch | last post by:
Hi all, I've got a server that handles a relatively high number of concurrent transactions (on the magnitude of 1000's per second). Client applications establish socket connections with the...
6
by: thomasp | last post by:
For those who gave advice on the shortfalls of my first attempt at writing a vb.net class, Thank You. I hope that I was able to apply some of your advice to this larger atempt. At first I didn' t...
68
by: Martin Joergensen | last post by:
Hi, I have some files which has the following content: 0 0 0 0 0 0 0 1 1 1 1 0 0 1 1 1 1 0 0 1 1 1 1 0 0 1 1 1 1 0 0 0 0 0 0 0
3
by: junaidnaseer | last post by:
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...
19
by: VK | last post by:
http://groups.google.com/group/comp.lang.javascript/browse_frm/thread/ b495b4898808fde0> is more than one month old - this may pose problem for posting over some news servers. This is why I'm...
10
by: coaassign | last post by:
Hi all I need to get a count the no of instructions executed in a for loop in exactly one second... Can any one please give me the code for this... THANKS IN ADVANCE
2
by: alwaali | last post by:
Hi I need help please This is my project and i need a help to solve it with you A page of text is to be read and analyzed to determine number of occurrences and locations of different words. The...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.