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

efficiency question - nested function cals

Hello,

Are nested function calls inefficient (or inherently evil)? In Delphi
I've seen small but significant performance improvements by always
declaring a local variable and 'unwrapping' the nested calls, but I'm not
convinced it is necessarily so in c#. So for example:

public struct TideValue {
public TideValue( double Time, double Height )
{
time = Time;
height = Height;
}
public double time;
public double height;
}

public class TideClass {
private ArrayList Values;
private double GetTideValue ( double aTime )
{
do something
return something
}
public void CalcTides( double starttime, double endtime )
{
while ( starttime < endtime )
{
Values.Add( new TideValue( curtime, GetInstantHeight( curtime ) )
);
}
}
}

or will this be more efficient;
public void CalcTides( double starttime, double endtime )
{
while ( starttime < endtime )
{
double height = GetInstantHeight( curtime );
TideValue value = new TideValue( curtime, height )
Values.Add( value );
}
}

I'm going to be thousands of these, so small improvements addup. I will,
of course, be doing some tests once the code works, but I'm curious to
know what the rules are for this sort of thing in general.

Thanks

Marc Pelletier
Nov 15 '05 #1
6 1527
Marc Pelletier <no******@please.com> wrote:
Are nested function calls inefficient (or inherently evil)? In Delphi
I've seen small but significant performance improvements by always
declaring a local variable and 'unwrapping' the nested calls, but I'm not
convinced it is necessarily so in c#.
No, I suspect they will be JIT compiled to the same code.
I'm going to be thousands of these, so small improvements addup.


Possibly - but is it really likely to be your performance bottleneck?

I would personally write the code in the most readable fashion
possible, and only worry about this type of micro-optimisation *if* you
end up with a performance problem - and then certainly only change the
code after making sure that not only does it improve performance, but
that it improves it enough to be worth a loss in readability.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #2
Jon Skeet [C# MVP] <sk***@pobox.com> wrote in
news:MP************************@msnews.microsoft.c om:
No, I suspect they will be JIT compiled to the same code.


You are right. On 10,000 iterations the execution time is virtually the
same.

But I disagree with your other assertion, that I shouldn't worry about
those issues until I find a bottleneck. I am just learning csharp and this
is surely the time to establish good practices in this language. Good
coding style, once established, can carry you far. For example the
execution time in this case is the same, but the unnested version is much
easier to debug.

I've done a few more optimisation tests that gave very confusing results...
see my other post.

Thanks

Marc Pelletier
Nov 15 '05 #3
Jon Skeet [C# MVP] <sk***@pobox.com> wrote in
news:MP************************@msnews.microsoft.c om:
No, I suspect they will be JIT compiled to the same code.


You are right. On 10,000 iterations the execution time is virtually the
same.

But I disagree with your other assertion, that I shouldn't worry about
those issues until I find a bottleneck. I am just learning csharp and this
is surely the time to establish good practices in this language. Good
coding style, once established, can carry you far. For example the
execution time in this case is the same, but the unnested version is much
easier to debug.

I've done a few more optimisation tests that gave very confusing results...
see my other post.

Thanks

Marc Pelletier
Nov 15 '05 #4
Marc Pelletier <no******@please.com> wrote in
news:Xn*********************@207.46.248.16:
I've done a few more optimisation tests that gave very confusing
results... see my other post.


Never mind, I cancelled the other post. It turns out that timing results
vary a great deal when run from the debugger compared to running
standalone. In fact when running from the debugger I was getting very
consistent results showing that order of execution of the tests affected
their timing, by factors of as much as 25%!

cheers

Marc
Nov 15 '05 #5
Marc Pelletier <no******@please.com> wrote in
news:Xn*********************@207.46.248.16:
I've done a few more optimisation tests that gave very confusing
results... see my other post.


Never mind, I cancelled the other post. It turns out that timing results
vary a great deal when run from the debugger compared to running
standalone. In fact when running from the debugger I was getting very
consistent results showing that order of execution of the tests affected
their timing, by factors of as much as 25%!

cheers

Marc
Nov 15 '05 #6
Marc Pelletier <no******@please.com> wrote:
You are right. On 10,000 iterations the execution time is virtually the
same.

But I disagree with your other assertion, that I shouldn't worry about
those issues until I find a bottleneck. I am just learning csharp and this
is surely the time to establish good practices in this language. Good
coding style, once established, can carry you far. For example the
execution time in this case is the same, but the unnested version is much
easier to debug.


So write that more readable code, as I suggested. If one version is
"much easier to debug", use that version until you find some *really
good* reason not to. It would be silly (IMO) to write less readable
code *everywhere* just because there's a very small chance that the
bottleneck in your code is due to one micro-optimisation. It's far more
likely to end up being either something you can't easily control
(bandwidth, another server, etc), an algorithm, or some combination of
the two (one particular way of doing something which eats bandwidth
when there's an alternative available). I can't remember the last time
a micro-optimisation like this was a problem.

(There's a big difference between this and the similar-looking decision
to use a StringBuilder when building up long strings. That's not a
micro-optimisation of syntax which you can reasonably hope the JIT can
compile away; it's a fundamental problem of creating too many objects.
Even that decision is only relevant when you know the number of strings
to append could end up being reasonably large (or more).)

It's important to get *architecture* right early on - that can make a
*huge* difference to performance, and is very hard to fix later on, but
not actual code.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #7

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

Similar topics

6
by: Daan | last post by:
Hello all, I have attempted to write my own forum using PHP and MySQL. It works, but the efficiency is far from ideal. It takes a long time to load e.g. the list with threads in a certain forum....
6
by: Steve M | last post by:
1. Near the beginning of the document "Unifying types and classes in Python 2.2" by GvR, which can be found at http://www.python.org/2.2.2/descrintro.html, the author writes the following about...
6
by: Andy Baker | last post by:
Hi there, I'm learning Python at the moment and trying to grok the thinking behind it's scoping and nesting rules. I was googling for nested functions and found this Guido quote:...
37
by: Tim N. van der Leeuw | last post by:
Hi, The following might be documented somewhere, but it hit me unexpectedly and I couldn't exactly find this in the manual either. Problem is, that I cannot use augmented assignment operators...
19
by: vamshi | last post by:
Hi all, This is a question about the efficiency of the code. a :- int i; for( i = 0; i < 20; i++ ) printf("%d",i); b:- int i = 10;
0
by: RussellKay | last post by:
To all who might be interested: I have created a CALS to HTML XSL transformation style sheet that converts 98% of the CALS table standard to a HTML equivalent. The XSL Version 1.0 transformation...
3
by: Luna Moon | last post by:
My friend has three nested loops, each has 10000, 1000, 100 iterations, respectively. What should be the most efficient way of layout out the three nested loops? for (i=0; i<10000; i++) for...
4
by: Wolfgang Draxinger | last post by:
If you know languages like Python or D you know, that nested functions can be really handy. Though some compilers (looking at GCC) provide the extension of nested functions, I wonder, how one...
5
by: want.to.be.professer | last post by:
For OO design, I like using virtual member function.But considering efficiency, template is better. Look at this program, class Animal { public: virtual void Walk() = 0; }; class Dog
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.