HighTechTalks DotNet Forums  

Re: string.<insertStaticMethodHere> performance in multithreaded envir

Dotnet Framework (Performance) microsoft.public.dotnet.framework.performance


Discuss Re: string.<insertStaticMethodHere> performance in multithreaded envir in the Dotnet Framework (Performance) forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
Jon Skeet [C# MVP]
 
Posts: n/a

Default Re: string.<insertStaticMethodHere> performance in multithreaded envir - 04-18-2006 , 02:09 PM






dave.dolan <davedolan (AT) discussions (DOT) microsoft.com> wrote:
Quote:
The old saying goes something like, "Hey stupid, don't use static methods
when you're going to call that function a lot from multiple threads, if you
want to have any kind of good performance" at least I think that's how it
goes.
I don't know who says that.

Quote:
Anyway, I get the idea behind it, because it's locking the single instance
of code in the entire app domain for now and all the life of the app, and
everyone accessing it has to wait in line.
Only if there's synchronization involved. If there isn't (and usually
there needn't be) all the threads can run through the same method at
the same time. It's easy to verify this yourself:

using System;
using System.Threading;

public class Test
{
static void Main()
{
for (int i=0; i < 5; i++)
{
new Thread(new ThreadStart(Count)).Start();
}
}

static void Count()
{
for (int i=0; i < 10; i++)
{
Console.WriteLine (i);
Thread.Sleep(200);
}
}
}

If static methods had to queue all the requests up, you'd see
0
1
2
3
4
5
6
7
8
9

five times. Instead, you're more likely to see
0
0
0
0
0
1
1
1
1
1
2
2
2
2
2

(etc)

--
Jon Skeet - <skeet (AT) pobox (DOT) com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too


Reply With Quote
  #2  
Old   
dave.dolan
 
Posts: n/a

Default Re: string.<insertStaticMethodHere> performance in multithreaded e - 04-18-2006 , 02:34 PM






Ok, thank you very much, I feel better after having read that!

I'm glad I asked instead of proceeding with my misinformation.

"Jon Skeet [C# MVP]" wrote:

Quote:
dave.dolan <davedolan (AT) discussions (DOT) microsoft.com> wrote:
The old saying goes something like, "Hey stupid, don't use static methods
when you're going to call that function a lot from multiple threads, if you
want to have any kind of good performance" at least I think that's how it
goes.

I don't know who says that.

Anyway, I get the idea behind it, because it's locking the single instance
of code in the entire app domain for now and all the life of the app, and
everyone accessing it has to wait in line.

Only if there's synchronization involved. If there isn't (and usually
there needn't be) all the threads can run through the same method at
the same time. It's easy to verify this yourself:

using System;
using System.Threading;

public class Test
{
static void Main()
{
for (int i=0; i < 5; i++)
{
new Thread(new ThreadStart(Count)).Start();
}
}

static void Count()
{
for (int i=0; i < 10; i++)
{
Console.WriteLine (i);
Thread.Sleep(200);
}
}
}

If static methods had to queue all the requests up, you'd see
0
1
2
3
4
5
6
7
8
9

five times. Instead, you're more likely to see
0
0
0
0
0
1
1
1
1
1
2
2
2
2
2

(etc)

--
Jon Skeet - <skeet (AT) pobox (DOT) com
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too


Reply With Quote
  #3  
Old   
Jon Skeet [C# MVP]
 
Posts: n/a

Default Re: string.<insertStaticMethodHere> performance in multithreaded e - 04-18-2006 , 02:55 PM



dave.dolan <davedolan (AT) discussions (DOT) microsoft.com> wrote:
Quote:
Ok, thank you very much, I feel better after having read that!

I'm glad I asked instead of proceeding with my misinformation.
It's always good to check if something sounds dodgy

(Sorry if I wrote a bit harshly before - not sure what came over me.)

--
Jon Skeet - <skeet (AT) pobox (DOT) com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too


Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.