473,387 Members | 1,517 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,387 software developers and data experts.

efficient assignment ???

Is this:

int x;
for (int count=0; count<200; count++)
{
x = someFunc();
anotherFunc(x);
}
more efficient than this:

for (int count=0; count<200; count++)
{
int x = someFunc();
anotherFunc(x);
}

can someone also explain why?
thanks.
Jul 22 '05 #1
12 1422
Programmer wrote:

Is this:

int x;
for (int count=0; count<200; count++)
{
x = someFunc();
anotherFunc(x);
}

more efficient than this:

for (int count=0; count<200; count++)
{
int x = someFunc();
anotherFunc(x);
}

can someone also explain why?
thanks.


Try it with your compiler. You can use the timing
functions clock() to figure this one out

eg.

#include <cstdio>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()
{
clock_t start = clock();

// your code goes here

clock_t finish = clock();

double duration = (double)(finish - start) / CLOCKS_PER_SEC;
printf( "%2.f seconds\n", duration );
}

But make sure, that the duration is long enough. Usually just reapeating
what you need to time a few million times makes it long enough, such
that the resotion of clock() doesn't matter any more (In other words:
make sure your program runs at least a few seconds).
--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #2


Programmer wrote:
Is this:

int x;
for (int count=0; count<200; count++)
{
x = someFunc();
anotherFunc(x);
}
more efficient than this:

for (int count=0; count<200; count++)
{
int x = someFunc();
anotherFunc(x);
}

can someone also explain why?
thanks.


You'll probably find that for PODs the compiler will optimize example 2
to be similar to example 1. This won't be the same for non POD
variables. I've seen as much as 17% slowdown for code that looked like:

for (int count=0; count<200; count++)
{
myClass a = someFunc();
anotherFunc(a);
}

run the profiler.

Jul 22 '05 #3
> Is this:

int x;
for (int count=0; count<200; count++)
{
x = someFunc();
anotherFunc(x);
}
more efficient than this:

for (int count=0; count<200; count++)
{
int x = someFunc();
anotherFunc(x);
}


The second format will be optimized by your compiler to the first one.
I'd say the second runs as fast as the first, since declarations don't
need any processing.

regards,
Bart

Jul 22 '05 #4
Programmer wrote:
Is this:

int x;
for (int count=0; count<200; count++)
{
x = someFunc();
anotherFunc(x);
}
more efficient than this:

for (int count=0; count<200; count++)
{
int x = someFunc();
anotherFunc(x);
}

can someone also explain why?


The second is more efficient in terms of programming time. The most
important resource to optimize is programmer time. Do not worry about which
statements are more efficient, worry about how likely they are to catch a
bug. The first version is more risky because you might find yourself using x
outside that loop, and not notice that inside the loop x gets changed.

Always give identifiers the narrowest scope possible.

After you write a big program (and all its automated tests), you can time
them to find the real performance bottleneck. Replacing algorithms will have
a much bigger effect on performance than individual integers.

In this specific case, the compiler will most likely optimize. If it puts x
on the stack, x's address reserves when the function enters, as if x had
been declared outside the loop. If it puts x into a register, then it
doesn't reserve any stack. You will never be able to time a difference
between these minor adjustments.

--
Phlip
http://industrialxp.org/community/bi...UserInterfaces
Jul 22 '05 #5
Programmer wrote:
Is this:

int x;
for (int count=0; count<200; count++)
{
x = someFunc();
anotherFunc(x);
}
more efficient than this:

for (int count=0; count<200; count++)
{
int x = someFunc();
anotherFunc(x);
}


Both are less efficient than

for (int count = 0; count < 200; ++count)
anotherFunc(someFunc());

V
Jul 22 '05 #6
Victor Bazarov wrote:
Programmer wrote:
Is this:

int x;
for (int count=0; count<200; count++)
{
x = someFunc();
anotherFunc(x);
}
more efficient than this:

for (int count=0; count<200; count++)
{
int x = someFunc();
anotherFunc(x);
}


Both are less efficient than

for (int count = 0; count < 200; ++count)
anotherFunc(someFunc());


I'd expect all three to be the same, or at least not significantly
different on any real-world compiler.

Jul 22 '05 #7
Victor Bazarov <v.********@comAcast.net> wrote in message news:<4b************@newsread1.dllstx09.us.to.veri o.net>...
[quotes snipped]
Both are less efficient than

for (int count = 0; count < 200; ++count)
anotherFunc(someFunc());


Are you sure? It seems reasonable to expect an optimizing compiler
to generate identical object code for all three versions. VC does,
for example.
Jul 22 '05 #8
Niklas Borson wrote:
Victor Bazarov <v.********@comAcast.net> wrote in message news:<4b************@newsread1.dllstx09.us.to.veri o.net>...
[quotes snipped]
Both are less efficient than

for (int count = 0; count < 200; ++count)
anotherFunc(someFunc());

Are you sure? It seems reasonable to expect an optimizing compiler
to generate identical object code for all three versions. VC does,
for example.


Of course I am sure. How many fewer characters did I have to type
to get to the same object code? See?
Jul 22 '05 #9

"Bart Blommerde" <ba**@ilse.net> wrote in message
news:41***********************@diablo.nl.easynet.n et...

The second format will be optimized by your compiler to the first one.
I'd say the second runs as fast as the first, since declarations don't
need any processing.


Aren't these two sentences in slight logical conflict? I mean, if
one is as fast as the other, what would be the reason for the
optimizer to asymmetrically convert the second to the first one?

- Risto -
Jul 22 '05 #10
Niklas Borson wrote:
Victor Bazarov <v.********@comAcast.net> wrote in message news:<4b************@newsread1.dllstx09.us.to.veri o.net>...
[quotes snipped]
Both are less efficient than

for (int count = 0; count < 200; ++count)
anotherFunc(someFunc());

Are you sure? It seems reasonable to expect an optimizing compiler
to generate identical object code for all three versions. VC does,
for example.


Victor's example should compile faster (but whether
that's observable or not is another matter).

One issue with Victor's example is that the return
value from someFunc is hidden from a debugger and
hard to see (from most of them that I used). When
testing the code, I prefer to use separate
assignments. After the code works, I'll convert
it to Victor's example, if and only if the readability
is greater.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book

Jul 22 '05 #11
Victor Bazarov <v.********@comAcast.net> wrote in message news:<nn***********@newsread1.dllstx09.us.to.verio .net>...
Niklas Borson wrote:
Victor Bazarov <v.********@comAcast.net> wrote in message news:<4b************@newsread1.dllstx09.us.to.veri o.net>...
[quotes snipped]
Both are less efficient than

for (int count = 0; count < 200; ++count)
anotherFunc(someFunc());

Are you sure? It seems reasonable to expect an optimizing compiler
to generate identical object code for all three versions. VC does,
for example.


Of course I am sure. How many fewer characters did I have to type
to get to the same object code? See?


Fair enough. :-)
Jul 22 '05 #12
Thomas Matthews <Th****************************@sbcglobal.net> wrote in message news:<Am*****************@newssvr17.news.prodigy.c om>...
Niklas Borson wrote:
Victor Bazarov <v.********@comAcast.net> wrote in message news:<4b************@newsread1.dllstx09.us.to.veri o.net>...
[quotes snipped]
Both are less efficient than

for (int count = 0; count < 200; ++count)
anotherFunc(someFunc());

Are you sure? It seems reasonable to expect an optimizing compiler
to generate identical object code for all three versions. VC does,
for example.


Victor's example should compile faster (but whether
that's observable or not is another matter).

One issue with Victor's example is that the return
value from someFunc is hidden from a debugger and
hard to see (from most of them that I used). When
testing the code, I prefer to use separate
assignments. After the code works, I'll convert
it to Victor's example, if and only if the readability
is greater.


And of course, what one considers more readable is a
matter of style and opinion.

I used to favor really dense code. Over time, as
screens have gotten larger and my functions smaller,
I have come to prefer a less dense style. It's still
a judgment call in each case, but I am now more
likely than before to break up complex expressions
and assign intermediate results to named variables.
Jul 22 '05 #13

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

Similar topics

6
by: Narendra C. Tulpule | last post by:
Hi, if you know the Python internals, here is a newbie question for you. If I have a list with 100 elements, each element being a long string, is it more efficient to maintain it as a dictionary...
23
by: Paul Rubin | last post by:
OK, I want to scan a file for lines matching a certain regexp. I'd like to use an assignment expression, like for line in file: if (g := re.match(pat, line)): croggle(g.group(1)) Since...
10
by: Andrew Koenig | last post by:
It has been pointed out to me that various C++ books disagree about the relative precedence of ?: and the assignment operators. In order to satisfy myself about the matter once and for all, I...
5
by: CoolPint | last post by:
It seems to me that I cannot assign objects of a class which has a constant data member since the data member cannot be changed once the constructor calls are completed. Is this the way it is meant...
166
by: Graham | last post by:
This has to do with class variables and instances variables. Given the following: <code> class _class: var = 0 #rest of the class
6
by: Neil Zanella | last post by:
Hello, I would like to know whether the following C fragment is legal in standard C and behaves as intended under conforming implementations... union foo { char c; double d; };
20
by: TimeHorse | last post by:
I would like to gauge interest in the following proposal: Problem: Assignment statements cannot be used as expressions. Performing a list of mutually exclusive checks that require data...
8
by: Francisco | last post by:
Hello, Is there any code faster than this array position manipulation (some code omitted for brevity)?: internal struct TreeNodeTableItem { public int a; public int b; public int c; public...
7
by: Carlo Milanesi | last post by:
Hello, I just completed writing an online book about developing efficient software using the C++ language. You can find it here: http://en.wikibooks.org/wiki/Optimizing_C%2B%2B It is a wiki, that...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
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...
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,...

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.