Click here to Skip to main content
15,892,809 members
Home / Discussions / C#
   

C#

 
AnswerRe: Start and control a WebBrowser Pin
SimulationofSai8-May-07 4:38
SimulationofSai8-May-07 4:38 
GeneralRe: Start and control a WebBrowser Pin
apet808-May-07 21:35
apet808-May-07 21:35 
QuestionHow to do this Pin
Stoumann8-May-07 2:03
Stoumann8-May-07 2:03 
AnswerRe: How to do this Pin
slkr1718-May-07 2:12
slkr1718-May-07 2:12 
AnswerRe: How to do this Pin
_mubashir8-May-07 2:12
_mubashir8-May-07 2:12 
AnswerRe: How to do this Pin
J4amieC8-May-07 2:23
J4amieC8-May-07 2:23 
AnswerRe: How to do this Pin
Sathesh Sakthivel8-May-07 2:41
Sathesh Sakthivel8-May-07 2:41 
QuestionBenchmarking of C# Event Calls Pin
slkr1718-May-07 1:57
slkr1718-May-07 1:57 
Hello everybody!

Can someone tell me why the following code comes to the conclusion that using an event instead of a direct method call is about 3 times faster????

Its pretty simple, just copy it at give it a go!
------------------------------------------
public class Class1
{

event TestDelegate TestEvent;
delegate void TestDelegate(String test);

public static void Main(String[] args)
{
//Number of cycles
int n = 10000000;

Class1 c = new Class1();

//register events
c.TestEvent += c.Test;
c.TestEvent += c.Test1;
c.TestEvent += c.Test2;


Console.WriteLine("Calling event {0} times....", n);
DateTime startTime = DateTime.Now;

//Call the event n times
for (int i = 0; i < n;i++ )
{
if(c.TestEvent != null)
c.TestEvent("i = " + i);
}

DateTime stopTime = DateTime.Now;
TimeSpan duration = stopTime - startTime;



Console.WriteLine("Calling normal {0} times....", n);
startTime = DateTime.Now;

//Call the event n times
for (int i = 0; i < n; i++)
{
c.Test("i = " + i);
c.Test1("i = " + i);
c.Test2("i = " + i);
}

stopTime = DateTime.Now;


TimeSpan duration2 = stopTime - startTime;
Console.WriteLine("Event based call took: {0},{1} sec", duration.Seconds, duration.Milliseconds);
Console.WriteLine("Normal call took: {0},{1} sec", duration2.Seconds, duration2.Milliseconds);

double eventTime = duration.Seconds*1000 + duration.Milliseconds;
double normalTime = duration2.Seconds*1000 + duration2.Milliseconds;

Console.WriteLine("Normal calling is about {0} times faster.", eventTime/normalTime);


Console.ReadLine();
}


private void Test(String test)
{
//do sth
int i = 0;
int x = i * 2;
}

private void Test1(String test)
{
//do sth
int i = 0;
int x = i * 2;
}

private void Test2(String test)
{
//do sth
int i = 0;
int x = i * 2;
}
}
------------------------------------------

Thanks in advance!
AnswerRe: Benchmarking of C# Event Calls Pin
Luc Pattyn8-May-07 7:28
sitebuilderLuc Pattyn8-May-07 7:28 
GeneralRe: Benchmarking of C# Event Calls Pin
slkr1719-May-07 20:36
slkr1719-May-07 20:36 
Questionloop hashtable Pin
arkiboys8-May-07 1:41
arkiboys8-May-07 1:41 
AnswerRe: loop hashtable [modified] Pin
Martin#8-May-07 1:49
Martin#8-May-07 1:49 
GeneralRe: loop hashtable Pin
arkiboys8-May-07 4:01
arkiboys8-May-07 4:01 
AnswerRe: loop hashtable Pin
Martin#8-May-07 4:20
Martin#8-May-07 4:20 
GeneralRe: loop hashtable Pin
arkiboys8-May-07 4:42
arkiboys8-May-07 4:42 
GeneralRe: loop hashtable Pin
Martin#8-May-07 4:57
Martin#8-May-07 4:57 
QuestionNeed a masked (formatted) edit box which can work for date and time Pin
Mushtaque Nizamani8-May-07 1:30
Mushtaque Nizamani8-May-07 1:30 
AnswerRe: Need a masked (formatted) edit box which can work for date and time Pin
_mubashir8-May-07 1:39
_mubashir8-May-07 1:39 
AnswerRe: Need a masked (formatted) edit box which can work for date and time Pin
PIEBALDconsult8-May-07 9:47
mvePIEBALDconsult8-May-07 9:47 
AnswerRe: Need a masked (formatted) edit box which can work for date and time Pin
visualhint30-May-07 14:40
visualhint30-May-07 14:40 
Questionto display image from a location + c# Pin
kiran gedela8-May-07 1:28
kiran gedela8-May-07 1:28 
AnswerRe: to display image from a location + c# Pin
_mubashir8-May-07 1:34
_mubashir8-May-07 1:34 
QuestionRead from xml Pin
akkram8-May-07 1:14
akkram8-May-07 1:14 
AnswerRe: Read from xml Pin
lmoelleb8-May-07 1:20
lmoelleb8-May-07 1:20 
GeneralRe: Read from xml Pin
akkram8-May-07 1:25
akkram8-May-07 1:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.