Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Code size and Code execution time

Tags:

performance

c#

Is there any way to find what is a size of a code and what is its execution time so I can compare two codes and decide which is better?

For example lets say I want to find the size and execution time of this

Code 1

for(int i=0; i<5; i++)
{
   sum+=1;
}

and this

Code 2

for(int i=0; i<=4; i++)
{
   sum = sum + 1;
}

to decide which is better (I don't care about this example now). For example the result will be:

Code 1:
Size: ? KB
Time: ? ms

Code 2:
Size: ? KB
Time: ? ms
like image 253
a1204773 Avatar asked Jun 21 '12 20:06

a1204773


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.


1 Answers

You could either use ANTS Profiler http://www.red-gate.com/products/dotnet-development/ants-performance-profiler/ (payable product but they have a Trial version) or some other Profiler product (ANTS, vTune, OptimizeIt, DevPartner, YourKit, dotTrace) on the market.

You may also intstrument the functions by yourself by setting up some unit tests that execute those 2 functions using a manual StopWatch instrumentation to compare execution time (quicker and less expensive). Unit tests would also allow for assuring that you do not have any regressions on performance if you need to change the implementation later. http://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch.aspx

    var stopWatch = new Stopwatch();
    stopWatch.Start();
    var result = CallFunction();
    stopWatch.Stop();
    var executionTime = stopWatch.Elapsed;
like image 195
Jason De Oliveira Avatar answered Sep 23 '22 07:09

Jason De Oliveira