By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,962 Members | 1,997 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,962 IT Pros & Developers. It's quick & easy.

quite simple question on calling functions

P: n/a
does calling a regular function cost any cpu time? In other words, is it
faster to write the code of two functions into main(), or is it the exact
same thing as calling two functions. I know its nitty gritty but its
necessary for my program.
thanks
dave
Nov 15 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
if you are that worried about speed, i suggest you go
learn assembler and quit messing with a language that
runs thru the .net framework... aka compiled to IL before
being finally comiled to machine code as the app executes
thru the .net runtime.

yes there is over head in function calling, but for
christ sake what are you gaining other than a nasty
poorly designed app? if you you think it's slow because
you have two functions, you should seriously consider
rethinking what those functions are doing, and the types
of the parameters as well as how they are being passed.

passing large objects back and forth is going to eat ram
and run slower... that's pretty obvious.

can you provide a snippet of what you feel is causing the
slowdown? chances are that it's not just having two
functions, but more in their design.
-----Original Message-----
does calling a regular function cost any cpu time? In other words, is itfaster to write the code of two functions into main(), or is it the exactsame thing as calling two functions. I know its nitty gritty but itsnecessary for my program.
thanks
dave
.

Nov 15 '05 #2

P: n/a

"code6" <an*******@discussions.microsoft.com> wrote in message
news:06****************************@phx.gbl...
if you are that worried about speed, i suggest you go
learn assembler and quit messing with a language that
runs thru the .net framework... aka compiled to IL before
being finally comiled to machine code as the app executes
thru the .net runtime.

yes there is over head in function calling, but for
christ sake what are you gaining other than a nasty
poorly designed app? if you you think it's slow because
you have two functions, you should seriously consider
rethinking what those functions are doing, and the types
of the parameters as well as how they are being passed.
Its a relevent question, no need to go nuts over it.

However, I advise you write for maintainablility and worry about performance
at that level AFTER the app works. Two methods, while there MAY be some
minor overhead is pretty irrelevent over all. Write your methods in logical
divisions of duty.

For small, non-virtual methods, the JIT is free to, and probably will,
inline the code, eliminating any overhead from the call. In larger methods,
the execution of the method body is the major factor, not the time to call
into that method body.
Calling virtual or interface methods may have additional performance issues.

You may want to take a look at your looping, that is more likely an issue
than method calls. Your code is likely making alot more method calls than
you realize.
passing large objects back and forth is going to eat ram
and run slower... that's pretty obvious.
Actually, large or small, objects are referenced, not passed around by value

string MyFunction()
{
string LargeString = //"few megs of text loaded from somewhere";
return LargeString;
}

string a,b,c;
a=LargeString();
b=LargeString();
c=LargeString();

takes up no significant extra memory. however, large structures can cause
you problems.
can you provide a snippet of what you feel is causing the
slowdown? chances are that it's not just having two
functions, but more in their design.
-----Original Message-----
does calling a regular function cost any cpu time? In

other words, is it
faster to write the code of two functions into main(),

or is it the exact
same thing as calling two functions. I know its nitty

gritty but its
necessary for my program.
thanks
dave
.

Nov 15 '05 #3

P: n/a

"code6" <an*******@discussions.microsoft.com> wrote in message
news:06****************************@phx.gbl...
if you are that worried about speed, i suggest you go
learn assembler and quit messing with a language that
runs thru the .net framework... aka compiled to IL before
being finally comiled to machine code as the app executes
thru the .net runtime.

yes there is over head in function calling, but for
christ sake what are you gaining other than a nasty
poorly designed app? if you you think it's slow because
you have two functions, you should seriously consider
rethinking what those functions are doing, and the types
of the parameters as well as how they are being passed.

passing large objects back and forth is going to eat ram
and run slower... that's pretty obvious.

can you provide a snippet of what you feel is causing the
slowdown? chances are that it's not just having two
functions, but more in their design.
-----Original Message-----
does calling a regular function cost any cpu time? In

other words, is it
faster to write the code of two functions into main(),

or is it the exact
same thing as calling two functions. I know its nitty

gritty but its
necessary for my program.
thanks
dave
.

I'm just trying to figure out how to write the fastest program possible for
a research project which will include artifical intelligence and quantum
physics calculations. Its going to simulate chemical reactions on the
screen and in the mean time I plan to make it able to find easier ways to
calculate the same things because some of the calculations that it will do
will be redundant. that's what the artificial intelligence is for. but the
point is that the program will be hopelessly time consuming if it was fully
functional. In fact, it could take days if not longer to simulate something!
If you can suggest a good way to easily build a program that integrates
windows forms, directx graphics, and very fast code PLEASE PLEASE PLEASE
tell me. C# is just so easy to work with because it is visual but I also
know how to program in C and am only barely familiar with assembly.
thanks
dave
Nov 15 '05 #4

P: n/a

"Dave" <da*********@hotmail.com> wrote in message
news:xu********************@comcast.com...

"code6" <an*******@discussions.microsoft.com> wrote in message
news:06****************************@phx.gbl...
if you are that worried about speed, i suggest you go
learn assembler and quit messing with a language that
runs thru the .net framework... aka compiled to IL before
being finally comiled to machine code as the app executes
thru the .net runtime.

yes there is over head in function calling, but for
christ sake what are you gaining other than a nasty
poorly designed app? if you you think it's slow because
you have two functions, you should seriously consider
rethinking what those functions are doing, and the types
of the parameters as well as how they are being passed.

passing large objects back and forth is going to eat ram
and run slower... that's pretty obvious.

can you provide a snippet of what you feel is causing the
slowdown? chances are that it's not just having two
functions, but more in their design.
-----Original Message-----
does calling a regular function cost any cpu time? In other words, is it
faster to write the code of two functions into main(),

or is it the exact
same thing as calling two functions. I know its nitty

gritty but its
necessary for my program.
thanks
dave
.

I'm just trying to figure out how to write the fastest program possible

for a research project which will include artifical intelligence and quantum
physics calculations. Its going to simulate chemical reactions on the
screen and in the mean time I plan to make it able to find easier ways to
calculate the same things because some of the calculations that it will do
will be redundant. that's what the artificial intelligence is for. but the point is that the program will be hopelessly time consuming if it was fully functional. In fact, it could take days if not longer to simulate something! If you can suggest a good way to easily build a program that integrates
windows forms, directx graphics, and very fast code PLEASE PLEASE PLEASE
tell me. C# is just so easy to work with because it is visual but I also
know how to program in C and am only barely familiar with assembly.
thanks
dave
If you need blazing code, I'd suggest building a C\C++ library that does it
and talks to the winforms host via a chunky call. Unfortunatly there are
some issues with managed code that may cause performance degredation in
situations like yours(I'm not saying the performance will be bad, just that
you can probably do better).

Without knowing the specifics of your app, I assume you perform calculations
over some particular set of data and that the simulation processing is the
major chunk of code, not the display or data loading. In that case, I'd
write, to whatever level you need in C in a way that performs as much work
per call as possible and then interface with that in C# to perform the UI
work.

Basically, look into PInvoke(DllImportAttribute()) and see if that will suit
your needs. If it will, import the function calls you need(I'd suggest as
few as possible), and run it on a seperate thread, stoping occasionally to
issue information to the UI and restart processing.

Also, you may want to look into intel's C++ compiler, I've used it a time or
two on CRC and MD5 hashing code and seen perceptible performance increases,
it may be of value...or it may not, I don't know.

Nov 15 '05 #5

P: n/a
See the link in the thread I started about performance comparison between
C#, C++, and java.

Only you know exactly what you mean by "very fast code". Does that mean
"fast enough" given a powerful enough box, or does that mean you'll be
graded on the performance relative to the other students?

"Dave" <da*********@hotmail.com> wrote in message
news:xu********************@comcast.com...
I'm just trying to figure out how to write the fastest program possible for a research project which will include artifical intelligence and quantum
physics calculations. Its going to simulate chemical reactions on the
screen and in the mean time I plan to make it able to find easier ways to
calculate the same things because some of the calculations that it will do
will be redundant. that's what the artificial intelligence is for. but the point is that the program will be hopelessly time consuming if it was fully functional. In fact, it could take days if not longer to simulate something! If you can suggest a good way to easily build a program that integrates
windows forms, directx graphics, and very fast code PLEASE PLEASE PLEASE
tell me. C# is just so easy to work with because it is visual but I also
know how to program in C and am only barely familiar with assembly.
thanks
dave

Nov 15 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.