473,686 Members | 2,291 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Static vs Non-Static Function Performance

We're currently doing some tests to determine the performance of static vs
non-static functions, and we're coming up with some odd(in our opinion)
results. We used a very simple setup. One class had a static function, and
the one class had a non-static function. Both of these functions did the
exact same thing.

The test function:
public void Test(){
decimal y = 2;
decimal x = 3;
decimal d = 0;

for(decimal z = 0; z < 1000000;z++){
if(z % 2 == 0){
d = y*d;
} else {
d = x*d;
}
}
}

The test running code:
for (int i = 0; i < 1000; i++) {
StaticTest.Test ();
}

for (int i = 0; i < 1000; i++) {
NonStaticTest nst = new NonStaticTest() ;
nst.Test();
}

To our suprise the non-static approach ran in 15 minutes, and the static
approach ran in 17 minutes. We had figured that due to the need to
instantiate a new object with each call for the non-static approach, that
this way would take longer. We also tried running the non-static approach
first, and the static approach second, the results were the same. We ran
these tests 3 times each to make sure that we got a nice average.

Did we somehow bias the test towards the non-static approach? How is it that
with an object instantion going on, the non-static approach is able to run
in 2 minutes less time. This is of course a substantially large margin, and
could have some definite performance implications for those building
performance oriented code.

Thanks,
Steve
Nov 15 '05 #1
22 12032
Hi Steve,

There could be some optimisations going on that are giving false results.

--
Michael Culley
"Steve - DND" <ng@digitalnoth ing.com> wrote in message news:#w******** ******@tk2msftn gp13.phx.gbl...
We're currently doing some tests to determine the performance of static vs
non-static functions, and we're coming up with some odd(in our opinion)
results. We used a very simple setup. One class had a static function, and
the one class had a non-static function. Both of these functions did the
exact same thing.

The test function:
public void Test(){
decimal y = 2;
decimal x = 3;
decimal d = 0;

for(decimal z = 0; z < 1000000;z++){
if(z % 2 == 0){
d = y*d;
} else {
d = x*d;
}
}
}

The test running code:
for (int i = 0; i < 1000; i++) {
StaticTest.Test ();
}

for (int i = 0; i < 1000; i++) {
NonStaticTest nst = new NonStaticTest() ;
nst.Test();
}

To our suprise the non-static approach ran in 15 minutes, and the static
approach ran in 17 minutes. We had figured that due to the need to
instantiate a new object with each call for the non-static approach, that
this way would take longer. We also tried running the non-static approach
first, and the static approach second, the results were the same. We ran
these tests 3 times each to make sure that we got a nice average.

Did we somehow bias the test towards the non-static approach? How is it that
with an object instantion going on, the non-static approach is able to run
in 2 minutes less time. This is of course a substantially large margin, and
could have some definite performance implications for those building
performance oriented code.

Thanks,
Steve

Nov 15 '05 #2
"Michael Culley" <mc*****@NOSPAM optushome.com.a u> wrote in message
news:O%******** ********@TK2MSF TNGP10.phx.gbl. ..
Hi Steve,

There could be some optimisations going on that are giving false results.

--
Michael Culley


Any idea what these enhancements could be? They should be doing the same
thing, should they not?

Steve
Nov 15 '05 #3
"Steve - DND" <ng@digitalnoth ing.com> wrote in message news:#q******** ******@TK2MSFTN GP11.phx.gbl...
Any idea what these enhancements could be? They should be doing the same
thing, should they not?
I can see several posibilities. The compiler might work out that nothing is returned from the function so optimise out some of the
code in the function. It could treat the function as static because it doesn't use an module level variables and it might not even
bother creating the object at all. All of these are just guesses though.

--
Michael Culley
"Steve - DND" <ng@digitalnoth ing.com> wrote in message news:#q******** ******@TK2MSFTN GP11.phx.gbl... "Michael Culley" <mc*****@NOSPAM optushome.com.a u> wrote in message
news:O%******** ********@TK2MSF TNGP10.phx.gbl. ..
Hi Steve,

There could be some optimisations going on that are giving false results.

--
Michael Culley


Any idea what these enhancements could be? They should be doing the same
thing, should they not?

Steve

Nov 15 '05 #4
Thats because static methods are using locks to be Thread-safe. The always
do internally a Monitor.Enter() and Monitor.exit() to ensure Thread-safety.

--
cody

[Freeware, Games and Humor]
www.deutronium.de.vu || www.deutronium.tk
Nov 15 '05 #5
"Michael Culley" <mc*****@NOSPAM optushome.com.a u> wrote in message
news:un******** ******@TK2MSFTN GP09.phx.gbl...
"Steve - DND" <ng@digitalnoth ing.com> wrote in message news:#q******** ******@TK2MSFTN GP11.phx.gbl...
Any idea what these enhancements could be? They should be doing the same
thing, should they not?


I can see several posibilities. The compiler might work out that nothing

is returned from the function so optimise out some of the code in the function. It could treat the function as static because it doesn't use an module level variables and it might not even bother creating the object at all. All of these are just guesses though.


Looks like you were correct. There seemed to be some optimizations going on.
When I changed things up a bit, I got *very* different results(static
running in less than 50% of the time).

Thanks,
Steve
Nov 15 '05 #6
maybe object pooling ?

--
Telmo Sampaio
MCSE (4 and 2k), MCSA, MCSD (6 and .NET), MCDBA, MCT, SPS, STA, SCSE, SAT,
MSF Practitioner, ITIL Certified
te***********@h otmail.com
"Steve - DND" <ng@digitalnoth ing.com> wrote in message
news:#q******** ******@TK2MSFTN GP11.phx.gbl...
"Michael Culley" <mc*****@NOSPAM optushome.com.a u> wrote in message
news:O%******** ********@TK2MSF TNGP10.phx.gbl. ..
Hi Steve,

There could be some optimisations going on that are giving false results.
--
Michael Culley


Any idea what these enhancements could be? They should be doing the same
thing, should they not?

Steve

Nov 15 '05 #7
codymanix wrote:
Thats because static methods are using locks to be Thread-safe. The always
do internally a Monitor.Enter() and Monitor.exit() to ensure Thread-safety.


Are you sure? Do you have a pointer to some docs that describe this?

I find it hard to believe that it would do this automatically, since
it's easy enough to place the entire method body in a lock statement
block, and there are many cases where static methods would have no need
to use Monitors to ensure thread safety.

--
mikeb

Nov 15 '05 #8
He is right. Take a look at the Il code or google for it.

--
-----------
Got TidBits?
Get it here: www.networkip.net/tidbits
"mikeb" <ma************ @mailnull.com> wrote in message
news:u7******** ******@TK2MSFTN GP09.phx.gbl...
codymanix wrote:
Thats because static methods are using locks to be Thread-safe. The always do internally a Monitor.Enter() and Monitor.exit() to ensure Thread-safety.


Are you sure? Do you have a pointer to some docs that describe this?

I find it hard to believe that it would do this automatically, since
it's easy enough to place the entire method body in a lock statement
block, and there are many cases where static methods would have no need
to use Monitors to ensure thread safety.

--
mikeb

Nov 15 '05 #9
"Alvin Bruney" <vapordan_spam_ me_not@hotmail_ no_spamhotmail. com> wrote in message news:OM******** ******@tk2msftn gp13.phx.gbl...
He is right. Take a look at the Il code or google for it.

So static functions should be avoided where optimum speed is needed?

--
Michael Culley

Nov 15 '05 #10

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

Similar topics

12
4419
by: lothar | last post by:
re: 4.2.1 Regular Expression Syntax http://docs.python.org/lib/re-syntax.html *?, +?, ?? Adding "?" after the qualifier makes it perform the match in non-greedy or minimal fashion; as few characters as possible will be matched. the regular expression module fails to perform non-greedy matches as described in the documentation: more than "as few characters as possible"
3
12235
by: Mario | last post by:
Hello, I couldn't find a solution to the following problem (tried google and dejanews), maybe I'm using the wrong keywords? Is there a way to open a file (a linux fifo pipe actually) in nonblocking mode in c++? I did something ugly like --- c/c++ mixture --- mkfifo( "testpipe", 777);
25
7616
by: Yves Glodt | last post by:
Hello, if I do this: for row in sqlsth: ________pkcolumns.append(row.strip()) ________etc without a prior:
32
4510
by: Adrian Herscu | last post by:
Hi all, In which circumstances it is appropriate to declare methods as non-virtual? Thanx, Adrian.
4
3058
by: Dave | last post by:
I need to add the ability to drag from a Windows Form and drop into a non dotNet application. For example, having a generated image in my app that I wish to drag out into explorer as a friendly way to save it. I have tried creating the object that I place into the DoDragDrop() by inheriting the COM interfaces IDropSource and IDataObject with no luck. If anyone can help I am very much open to suggestions. Thanks in advance!
8
2464
by: John Hazen | last post by:
I want to match one or two instances of a pattern in a string. According to the docs for the 're' module ( http://python.org/doc/current/lib/re-syntax.html ) the '?' qualifier is greedy by default, and adding a '?' after a qualifier makes it non-greedy. > The "*", "+", and "?" qualifiers are all greedy... > Adding "?" after the qualifier makes it perform the match in > non-greedy or minimal fashion...
5
2171
by: Joe | last post by:
I have an application which runs in a non-secure environment. I also have an application that runs in a secure environment (both on the same machine). Is there any way to share the session data for this? Most of the site allows the user to add things to a cart (non-secure), once they choose to check-out, I need this information which was stored in the session to be read by the payment page(secured). Hope this makes sense. It's probably...
0
2339
by: amitvps | last post by:
Secure Socket Layer is very important and useful for any web application but it brings some problems too with itself. Handling navigation between secure and non-secure pages is one of the cumbersome jobs. When a non-secure page references a secure page with relative URL, the web server generates error until absolute URL with https prefix is used. On the other hand when a secure page references a non-secure page, the non-secure page will be...
399
12812
by: =?UTF-8?B?Ik1hcnRpbiB2LiBMw7Z3aXMi?= | last post by:
PEP 1 specifies that PEP authors need to collect feedback from the community. As the author of PEP 3131, I'd like to encourage comments to the PEP included below, either here (comp.lang.python), or to python-3000@python.org In summary, this PEP proposes to allow non-ASCII letters as identifiers in Python. If the PEP is accepted, the following identifiers would also become valid as class, function, or variable names: Löffelstiel,...
12
29874
by: puzzlecracker | last post by:
is it even possible or/and there is a better alternative to accept input in a nonblocking manner?
0
8517
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8780
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7603
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6441
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4309
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4534
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2947
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 we have to send another system
2
2208
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
1940
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.