473,569 Members | 2,698 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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,compari sons ,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 2404
ju**********@gm ail.com wrote:
I had asked that I wanted to find the number of operations ( like
additions ,subtractions ,shifts,compari sons ,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**********@gm ail.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**********@gm ail.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 "instrumentatio n."
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_amou nt; 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 PrimitiveTypeHo lder {
private:

T value;

public:

static Counter counter;

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

value += rhs;

return *this;
}

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

};
Then, I would use macro trickery to turn:

int a;
float b;
double c;
into:

PrimitiveTypeHo lder<int> a;
PrimitiveTypeHo lder<float> b;
PrimitiveTypeHo lder<double> c;
I'm not sure if the following macro would work:

#define int PrimitiveTypeHo lder<int>
And then at the end of your program, you can check the total amount of
operations:

cout << Counter::GetTot alAmount();
Or just for a given type:

cout << PrimitiveTypeHo lder<float>().c ounter.GetAmoun t();
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
1478
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 limited set of IP addresses. 3) How to count the total number of request from each IP address per day.
4
5421
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 server. Data is sent and received over these connections using the asynchronous model. The server is currently in beta testing. Sporadically over the...
6
1435
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 really see an advantage of a Class over a module containing the same functions, but now that this Class is working for me, I have found a possible...
68
6770
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
4983
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 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...
19
3583
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 starting a new one] I'd still like to finish this rounding mess. As a startup lemma we can take that VK is the worst programmer of all times and...
10
6582
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
3401
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 results of the analysis are to be stored in a suitable data structure. The main task in this project is to design a suitable ADT (call it WAnalysis)...
0
7698
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7612
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8122
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
6284
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5513
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5219
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3653
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
937
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.