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

Timing of functions in C#

I am working on a rather large program that hits several methods
multiple times. In looking for ways to speed it up, I have discovered
something odd. The first time I call a method, it's about 30 times
slower than any subsequent call. This is true for all the methods that
I have tested. For example, the first call might take 30 milliseconds
whereas all subsequent calls take less than 1 ms. I would like to
understand why this is happening.

An example method looks like this:

public void computeVecP () // Escobal 5.3
{
VecP.X = (SatObs.Sat.KepElements.CosArgPerigee *
SatObs.Sat.KepElements.CosLongAsc) -
(SatObs.Sat.KepElements.SinArgPerigee *
SatObs.Sat.KepElements.SinLongAsc * SatObs.Sat.KepElements.CosInc);

VecP.Y = (SatObs.Sat.KepElements.CosArgPerigee *
SatObs.Sat.KepElements.SinLongAsc) +
(SatObs.Sat.KepElements.SinArgPerigee *
SatObs.Sat.KepElements.CosLongAsc * SatObs.Sat.KepElements.CosInc);

VecP.Z = SatObs.Sat.KepElements.SinArgPerigee *
SatObs.Sat.KepElements.SinInc;
}

Each term is "a property of a property of a property" and all values
have been pre-computed. In other words,
"SatObs.Sat.KepElements.CosArgPerigee" is declared as
public double CosArgPerigee{get{return cosArgPerigee;}
set{cosArgPerigee=value;}}

If I substitute simple variables (i.e. "X" for
"SatObs.Sat.KepElements.CosArgPerigee") then the time difference
disappears. Therefore I conclude that C# is spending a long time
locating the first property look-up but somehow doesn't need to do it
again for subsequent calls. I hope that I'm making sense here. Is
there a better or more complete explanation?

Nov 17 '05 #1
3 1773
The first time a method is called it is JIT'd. The Just In Time compiler
will compile the IDL, then will use the compiled version for subsequent
calls.

--
Floyd
"DrOrbit" <nl**@nlsa.com> wrote in message
news:11*********************@o13g2000cwo.googlegro ups.com...
I am working on a rather large program that hits several methods
multiple times. In looking for ways to speed it up, I have discovered
something odd. The first time I call a method, it's about 30 times
slower than any subsequent call. This is true for all the methods that
I have tested. For example, the first call might take 30 milliseconds
whereas all subsequent calls take less than 1 ms. I would like to
understand why this is happening.

An example method looks like this:

public void computeVecP () // Escobal 5.3
{
VecP.X = (SatObs.Sat.KepElements.CosArgPerigee *
SatObs.Sat.KepElements.CosLongAsc) -
(SatObs.Sat.KepElements.SinArgPerigee *
SatObs.Sat.KepElements.SinLongAsc * SatObs.Sat.KepElements.CosInc);

VecP.Y = (SatObs.Sat.KepElements.CosArgPerigee *
SatObs.Sat.KepElements.SinLongAsc) +
(SatObs.Sat.KepElements.SinArgPerigee *
SatObs.Sat.KepElements.CosLongAsc * SatObs.Sat.KepElements.CosInc);

VecP.Z = SatObs.Sat.KepElements.SinArgPerigee *
SatObs.Sat.KepElements.SinInc;
}

Each term is "a property of a property of a property" and all values
have been pre-computed. In other words,
"SatObs.Sat.KepElements.CosArgPerigee" is declared as
public double CosArgPerigee{get{return cosArgPerigee;}
set{cosArgPerigee=value;}}

If I substitute simple variables (i.e. "X" for
"SatObs.Sat.KepElements.CosArgPerigee") then the time difference
disappears. Therefore I conclude that C# is spending a long time
locating the first property look-up but somehow doesn't need to do it
again for subsequent calls. I hope that I'm making sense here. Is
there a better or more complete explanation?

Nov 17 '05 #2
Floyd Burger wrote:
The first time a method is called it is JIT'd. The Just In Time compiler
will compile the IDL, then will use the compiled version for subsequent
calls.


I think there was some tool that can be used to pre-compile the whole
program so it doesn't need to be just in time compiled. If the delay at the
first call is causing you problems you could try that. I can't remember
what the tool was called - try google!

Max

Nov 17 '05 #3
The longer time to execute is not a problem, but I was curious about
what was going on behind the scenes. Floyd's explanation makes sense.
I appreciate the responses.

Markus Stoeger wrote:
Floyd Burger wrote:
The first time a method is called it is JIT'd. The Just In Time compiler will compile the IDL, then will use the compiled version for subsequent calls.
I think there was some tool that can be used to pre-compile the whole
program so it doesn't need to be just in time compiled. If the delay

at the first call is causing you problems you could try that. I can't remember what the tool was called - try google!

Max


Nov 17 '05 #4

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

Similar topics

10
by: Greg Stark | last post by:
This query is odd, it seems to be taking over a second according to my log_duration logs and according to psql's \timing numbers. However explain analyze says it's running in about a third of a...
7
by: jamie | last post by:
hey all, I am attempting to do motion control for a final project, but I have a concern.... For motion control, timing is everyting, the better it is, the better it works. Currently I am...
1
by: Novice | last post by:
Hi all, I'm at my wit's end on trying to insert some timing code into the server side code that parses the hashed data contained in the hidden field being submitted to the server I've tried...
5
by: Steven D'Aprano | last post by:
I have a problem and I don't know where to start looking for a solution. I have a class that needs to call an arbitrary function and wait for a result. The function, being completely arbitrary...
2
by: Steven D'Aprano | last post by:
The timeit module is ideal for measuring small code snippets; I want to measure large function objects. Because the timeit module takes the code snippet argument as a string, it is quite handy...
1
by: Gary Coutts | last post by:
Hi, I need to find out the execution time of some methods. Can anyone tell what the best resolution I can except when timing routine. I really need sub millisecond accuracy. Cheers
1
by: MartyFromIreland | last post by:
Hi There! Run into a bit of a dilemma, I'm new to C# but I'm sure stacks of you will find this easy! Its regarding the timer functions, I've tried and failed numerous times as they just don't...
0
by: Daniel Fetchinson | last post by:
On 4/15/08, Daniel Fetchinson <fetchinson@googlemail.comwrote: BTW, using the following ###################################################################### # CODE TO TEST BOTH...
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...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...

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.