473,471 Members | 1,898 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

VERY ODD Function calling time

I did the following little test and was surprised by the results, why is the
"Initial : " time so slow ??? surely it should be faster than either of the
other two calls as it lacks a function call. I really dont get this...

i get the following output ....
Initial : 00:00:05.2031250
NonRef : 00:00:03.4062500
Ref : 00:00:04.1562500
Initial : 00:00:05.1406250
NonRef : 00:00:03.1875000
Ref : 00:00:03.9843750

Many thanks
Jon Rea

private void main()
{
int[] bob = new int[100000];
int repCount = 10000;
// initialise the bob array
for( int i = 0; i < length; i++ )
{
bob[i] = 2;
}
DateTime T1;
DateTime T2;
TimeSpan TS1;
// now loop many times trying different calls
while(true)
{

T1 = DateTime.Now; // record the time
for( int j = 0; j < repCount; j++ )
{
for( int i = 0; i < bob.Length; i++ )
{
bob[i] = 2;
}
}
T2 = DateTime.Now;
TS1 = T2 - T1;
Debug.WriteLine( "Initial : " + TS1.ToString() ); // for some reason this is
much slower than the two below ...
T1 = DateTime.Now;
for( int j = 0; j < repCount; j++ )
{
play( bob );
}
T2 = DateTime.Now;
TS1 = T2 - T1;
Debug.WriteLine( "NonRef : " + TS1.ToString() ); // why is this the fastes
method, surely the above is just an inline version of this ???
T1 = DateTime.Now;
for( int j = 0; j < repCount; j++ )
{
play( ref bob );
}
T2 = DateTime.Now;
TS1 = T2 - T1;
Debug.WriteLine( "Ref : " + TS1.ToString() );
}
}

// the following two function calls for interaction with the bob array, bob
is passed in one by ref in one and normally in the other

private void play( int[] b )
{
for( int i = 0; i < b.Length; i++ )
{
b[i] = 2;
}
}
private void play( ref int[] b )
{
for( int i = 0; i < b.Length; i++ )
{
b[i] = 2;
}
}
Nov 16 '05 #1
1 1198
I've disassemled the respective parts, and it seems to me like the non-Ref
version could enregister the counter variable i, while it's on the stack in
the other two versions.

Niki

"Jonathan Rea" <jo*****@bris.ac.uk> wrote in news:I3********@bath.ac.uk...
I did the following little test and was surprised by the results, why is
the
"Initial : " time so slow ??? surely it should be faster than either of
the
other two calls as it lacks a function call. I really dont get this...

i get the following output ....
Initial : 00:00:05.2031250
NonRef : 00:00:03.4062500
Ref : 00:00:04.1562500
Initial : 00:00:05.1406250
NonRef : 00:00:03.1875000
Ref : 00:00:03.9843750

Many thanks
Jon Rea

private void main()
{
int[] bob = new int[100000];
int repCount = 10000;
// initialise the bob array
for( int i = 0; i < length; i++ )
{
bob[i] = 2;
}
DateTime T1;
DateTime T2;
TimeSpan TS1;
// now loop many times trying different calls
while(true)
{

T1 = DateTime.Now; // record the time
for( int j = 0; j < repCount; j++ )
{
for( int i = 0; i < bob.Length; i++ )
{
bob[i] = 2;
}
}
T2 = DateTime.Now;
TS1 = T2 - T1;
Debug.WriteLine( "Initial : " + TS1.ToString() ); // for some reason this
is
much slower than the two below ...
T1 = DateTime.Now;
for( int j = 0; j < repCount; j++ )
{
play( bob );
}
T2 = DateTime.Now;
TS1 = T2 - T1;
Debug.WriteLine( "NonRef : " + TS1.ToString() ); // why is this the fastes
method, surely the above is just an inline version of this ???
T1 = DateTime.Now;
for( int j = 0; j < repCount; j++ )
{
play( ref bob );
}
T2 = DateTime.Now;
TS1 = T2 - T1;
Debug.WriteLine( "Ref : " + TS1.ToString() );
}
}

// the following two function calls for interaction with the bob array,
bob
is passed in one by ref in one and normally in the other

private void play( int[] b )
{
for( int i = 0; i < b.Length; i++ )
{
b[i] = 2;
}
}
private void play( ref int[] b )
{
for( int i = 0; i < b.Length; i++ )
{
b[i] = 2;
}
}

Nov 16 '05 #2

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

Similar topics

4
by: Peter Moscatt | last post by:
Is it possible to write code and allow a function to be called within another like I have shown below ? Pete def populatelist(): f=open(_globals.appath + "dxcluster.svr","r") while true:
3
by: Ken | last post by:
hello, I would to know if it is possible to call an object in a function within a class. Meaning , In a class, A function X calling onto a function Y, and function Y we want one of the two...
3
by: Janross | last post by:
I'm having trouble with a query that's prohibitively slow. On my free-standing office computer it's fine (well, 2-4 seconds), but on the client's network, it takes at least 5 minutes to run. ...
0
by: Diego | last post by:
Hello. I need help. I'm writing an application that requires audio recording and I have decided to use waveform audio interface. I'm able to process WIM_DATA message but when i call waveInStop the...
12
by: leaf | last post by:
Hi, How to call function at runtime, based on a struct that contains the information for the function call: struct func_to_call { int function_id; // function id to call unsigned int nparams;...
4
by: Peter Afonin | last post by:
Hello, I have a weirdest issue I've ever had. I have a function that enters some data into the Oracle table and returns the sequential row number for the new record (autonumber): Private...
5
by: Snis Pilbor | last post by:
Hello, Is there any actual difference between calling a function directly by name, vs. calling it by a function pointer which points to it? I read at the FAQ that the function name can be...
11
by: ypjofficial | last post by:
Hello All, So far I have been reading that in case of a polymorphic class ( having at least one virtual function in it), the virtual function call get resolved at run time and during that the...
3
by: william | last post by:
My situation is here: an array of two dimension can only be defined locally within a function(because the caller don't know the exact size ). Then the question is: how should the caller access...
126
by: jacob navia | last post by:
Buffer overflows are a fact of life, and, more specifically, a fact of C. All is not lost however. In the book "Value Range Analysis of C programs" Axel Simon tries to establish a...
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
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,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...
0
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...
0
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 ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.