August 16, 2005

Managed Code Performance

Today I read a very good article from a guy who works for Microsoft BCL Performance section. I found some good interesting concepts. If your interested about code performance you should read this article.

http://blogs.msdn.com/ricom/

I wrote a very simple performance test for testing the for vs foreach & string vs stringbuilder ..etc..etc.. you can find the code below.

Timer class: This calculates the code execution time.

///

/// To get the elapsed the time

///

public class MyTimer

{

long _start;

long _end ;

long _exeTime ;

public string ElapsedTime

{

get

{

//returns the Elapsed time

_exeTime = _end - _start;

return _exeTime.ToString();

}

}

///

/// Start the timer

///

public void Start()

{

_start = DateTime.Now.Ticks ;

}

///

/// End the timer

///

public void Stop()

{

_end= DateTime.Now.Ticks ;

}

}


Some of my test samples


static void Main(string[] args)

{

MyTimer m = new MyTimer();

ArrayList a = new ArrayList();

//For Tests

m.Start();

for(int i=0; i<1000;>

{

string sss= "as" + i.ToString();

Console.WriteLine(sss);

a.Add(sss);

}

m.Stop();

Console.WriteLine(m.ElapsedTime);


m.Start();

for(int i=0; i

{

Console.WriteLine(a[i].ToString());

}

m.Stop();

Console.WriteLine(m.ElapsedTime);

//Foreach Tests

m.Start();

foreach(string s in a)

{

Console.WriteLine(s);

}

m.Stop();

Console.WriteLine(m.ElapsedTime);

//String BuilderTest

StringBuilder name = new StringBuilder();

m.Start();

for(int i=0;i<100000;>

{

name.Append(i.ToString());

}

m.Stop();

Console.WriteLine(name.ToString());

Console.WriteLine(m.ElapsedTime);

}

6 comments:

Gogula G. Aryalingam said...

Can I use the same logic to calculate the execution time of a stored procedure I write in SQL Server?

Gogula G. Aryalingam said...

Yo Luddy... Where's my answer???

Ludmal De Silva said...

ohh sorry dude.. as u knw that im really busy doing BI stuff :-).. u can only check the code execution time. not the SQL server SP execution time. this is only for the code buddy.. time which the compiler takes to compile.

Gogula G. Aryalingam said...

What about in SQL Server 2005 then? With all this CLR integration and all... Do you think that it can be used here...

mandyprehiem9868 said...

='Brand New News Fr0m The Timber Industry!!'=

========Latest Profile==========
Energy & Asset Technology, Inc. (EGTY)
Current Price $0.15
================================

Recognize this undiscovered gem which is poised to jump!!

Please read the following Announcement in its Entierty and
Consider the Possibilities�
Watch this One to Trad,e!

Because, EGTY has secured the global rights to market
genetically enhanced fast growing, hard-wood trees!

EGTY trading volume is beginning to surge with landslide Announcement.
The value of this Stoc,k appears poised for growth! This one will not
remain on the ground floor for long.

KEEP READING!!!!!!!!!!!!!!!

===============
"BREAKING NEWS"
===============

-Energy and Asset Technology, Inc. (EGTY) owns a global license to market
the genetically enhanced Global Cedar growth trees, with plans to
REVOLUTIONIZE the forest-timber industry.

These newly enhanced Globa| Cedar trees require only 9-12 years of growth
before they can be harvested for lumber, whereas worldwide growth time for
lumber is 30-50 years.

Other than growing at an astonishing rate, the Global Cedar has a number
of other benefits. Its natural elements make it resistant to termites, and
the lack of oils and sap found in the wood make it resistant to forest fire,
ensuring higher returns on investments.
T
he wood is very lightweight and strong, lighter than Poplar and over twice
as strong as Balsa, which makes it great for construction. It also has
the unique ability to regrow itself from the stump, minimizing the land and
time to replant and develop new root systems.

Based on current resources and agreements, EGTY projects revenues of $140
Million with an approximate profit margin of 40% for each 9-year cycle. With
anticipated growth, EGTY is expected to challenge Deltic Timber Corp. during
its initial 9-year cycle.

Deltic Timber Corp. currently trades at over $38.00 a share with about $153
Million in revenues. As the reputation and demand for the Global Cedar tree
continues to grow around the world EGTY believes additional multi-million
dollar agreements will be forthcoming. The Global Cedar nursery has produced
about 100,000 infant plants and is developing a production growth target of
250,000 infant plants per month.

Energy and Asset Technology is currently in negotiations with land and business
owners in New Zealand, Greece and Malaysia regarding the purchase of their popular
and profitable fast growing infant tree plants. Inquiries from the governments of
Brazil and Ecuador are also being evaluated.

Conclusion:

The examples above show the Awesome, Earning Potential of little
known Companies That Explode onto Investor�s Radar Screens.
This s-t0ck will not be a Secret for long. Then You May Feel the Desire to Act Right
Now! And Please Watch This One Trade!!


GO EGTY!


All statements made are our express opinion only and should be treated as such.
We may own, take position and sell any securities mentioned at any time. Any
statements that express or involve discussions with respect to predictions,
goals, expectations, beliefs, plans, projections, object'ives, assumptions or
future events or perfo'rmance are not
statements of historical fact and may be
"forward,|ooking statements." forward,|ooking statements are based on expectations,
estimates and projections at the time the statements are made that involve a number
of risks and uncertainties which could cause actual results or events to differ
materially from those presently anticipated. This newsletter was paid $3,000 from
third party (IR Marketing). Forward,|ooking statements in this action may be identified
through the use of words such as: "pr0jects", "f0resee", "expect

Ludmal De Silva said...

sorry 4 late reply gogula. with sql 2005 u can write code behind stored procedures.. u can check the store proc executing time. Its CLR intergrated. for better info u should read this article its really good buddhy.. cheers,
http://msdn.microsoft.com/data/default.aspx?pull=/library/en-us/dnsql90/html/sqlclrguidance.asp