code optimization

K

Kovan Akrei

Hi,
The code shown below is going to be a part of some tests I'm doing. I'm
trying to check how many threads are created and started (threads does not
do anything and exits soon after creation) i a certain amount of time, i ex.
30 seconds.
I'm trying to optimize it as much as possible. I wonder if there is another
way to check systemtime insted of DateTime, beacause I think Datetime slows
down the tests. I'm only interested in those possiblities csharp and .net
presents.

class ThreadOverhead

{


static void Main(string[] args)

{

DateTime startTime;

long maxThread = 0;

TimeSpan endTime = new TimeSpan(0,0,30);

startTime = DateTime.Now;

while(((DateTime.Now)-startTime) < endTime)

{

new DummyThread().thread.Start();

++maxThread ;

}


}

}
 
N

Nicholas Paldino [.NET/C# MVP]

Kovan,

The only ways I can think of doing this without an API or COM call is
either using the TickCount on the Environment class (which probably just
calls GetTickCount anyways), or by using a performance counter (which one, I
am not sure). If you want to get more granularity, then you are going to
have to use an API call most likely.

Hope this helps.
 
C

Chris Jackson

Well, let's see what can be done.

First of all, you can modify your code so that it doesn't have to do a
computation with every iteration. If you set up a timer and set its interval
at whatever you want to measure. Then you implement the handler for this
timer to end your testing. If you want to then go on and execute some logic,
you'll have to figure out a way to signal your loop to end. If you want to
be rude, you could just kill the thread it is running on. Otherwise, I'm not
sure how you can avoid at least one comparison (if end == true) on each
iteration, which introduces less measurement error but it is still there.
Don't forget to synchronize properly, depending on which timer class you
decide to use (there are three), as not all are thread safe.

Just throwing out ideas...
 
K

Kovan Akrei

Interesting apraoch. I'll try to program this in my code, but I think the
results will some how be critisized as I'm trying to compare CSharp and
Java.
I have a Java version of the code written below and I use
System.currentTimeMillis(). That is why I use DateTime or Environment
namespace as Nicholas Paldino sugested (reply to the original post).

Thanks

Kovan Akrei

Chris Jackson said:
Well, let's see what can be done.

First of all, you can modify your code so that it doesn't have to do a
computation with every iteration. If you set up a timer and set its interval
at whatever you want to measure. Then you implement the handler for this
timer to end your testing. If you want to then go on and execute some logic,
you'll have to figure out a way to signal your loop to end. If you want to
be rude, you could just kill the thread it is running on. Otherwise, I'm not
sure how you can avoid at least one comparison (if end == true) on each
iteration, which introduces less measurement error but it is still there.
Don't forget to synchronize properly, depending on which timer class you
decide to use (there are three), as not all are thread safe.

Just throwing out ideas...

--
Chris Jackson
Software Engineer
Microsoft MVP - Windows Client
Windows XP Associate Expert
--
More people read the newsgroups than read my email.
Reply to the newsgroup for a faster response.
(Control-G using Outlook Express)
--

Kovan Akrei said:
Hi,
The code shown below is going to be a part of some tests I'm doing. I'm
trying to check how many threads are created and started (threads does not
do anything and exits soon after creation) i a certain amount of time, i
ex.
30 seconds.
I'm trying to optimize it as much as possible. I wonder if there is
another
way to check systemtime insted of DateTime, beacause I think Datetime
slows
down the tests. I'm only interested in those possiblities csharp and ..net
presents.

class ThreadOverhead

{


static void Main(string[] args)

{

DateTime startTime;

long maxThread = 0;

TimeSpan endTime = new TimeSpan(0,0,30);

startTime = DateTime.Now;

while(((DateTime.Now)-startTime) < endTime)

{

new DummyThread().thread.Start();

++maxThread ;

}


}

}
 
K

Kovan Akrei

Thanks for replying so fast. I have tried to use Environment class, but
there was no cignificant improvement results. I'm trying to compare CSharp
to Java. The comparison is regarded to parallell programing and the
performance of both system on regards to threads. I have written a little
more about this in a reply to a post posten by Chris Jackson on tuesday
09.03.04

Regards from
Kovan Akrei

Nicholas Paldino said:
Kovan,

The only ways I can think of doing this without an API or COM call is
either using the TickCount on the Environment class (which probably just
calls GetTickCount anyways), or by using a performance counter (which one, I
am not sure). If you want to get more granularity, then you are going to
have to use an API call most likely.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Kovan Akrei said:
Hi,
The code shown below is going to be a part of some tests I'm doing. I'm
trying to check how many threads are created and started (threads does not
do anything and exits soon after creation) i a certain amount of time, i ex.
30 seconds.
I'm trying to optimize it as much as possible. I wonder if there is another
way to check systemtime insted of DateTime, beacause I think Datetime slows
down the tests. I'm only interested in those possiblities csharp and ..net
presents.

class ThreadOverhead

{


static void Main(string[] args)

{

DateTime startTime;

long maxThread = 0;

TimeSpan endTime = new TimeSpan(0,0,30);

startTime = DateTime.Now;

while(((DateTime.Now)-startTime) < endTime)

{

new DummyThread().thread.Start();

++maxThread ;

}


}

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top