Comparison, VBA, C++ and C#
-
Hello, I was looking at monte-carlo techniques and found a description of a method for estimating the number PI. I was in work at the time and so i opened Excel wrote it out in VBA and played around trying to see how many iterations it took to get a decent figure. This made me think of preformance issues when making a large number of iterations. At home now i've written the same thing out in C++ and C# and have compared the excecution time and was suprised to find that C# gave a slightly quicker time than C++. I always thought that with the bulk of .net C# would be slower than C++. Here are the approx times(seconds) for 1,000,000,000 iterations. C++ 232 VBA 2122 C# 201 Now i come to my point. Is it possible for you to look at the code below(possibly run it) and see if i have made a fair comparison. I haven't researched the best methods for doing this i've just written it in the first way that worked. I am book-self-taught so i am very likely making serious mistakes so if you see any glaring miatakes please point them out. I'm also interested in finding out how you would really go about testing the efficiency of algorithms if anyone has any advice. Thanks a lot; Sorry this is going to make this a long post: VBA ***********************
Private Const rad As Double = 1 'The radius of the circle. Private Const tries As Double = 1000000000 'The number of iterations Dim N_cir As Double 'The number of hits inside the circle(1/4 circle) Dim N_sq As Double 'The number of hits inside the square. Dim PI As Double 'The final value of PI calculated. Dim i As Double 'A counter Dim x As Double 'X-co-ordinate Dim y As Double 'Y-co-ordinate 'To assign a random number to the x and y co-ordinated Private Function Rand(max As Double) Rand = max * Rnd() End Function Sub FindPI() Dim start As String i = 0 start = Right(Now, 9) Do While i < tries x = Rand(rad) 'Assign random numbers to the y = Rand(rad) 'x and y co-ordinates 'If the random point falls in the circle then 'increment the number of hits in the circle. If ((x * x) + (y * y)) <= rad * rad Then N_cir = N_cir + 1 End If 'The hits will always fall in the square N_sq = N_sq + 1 i = i + 1 Loop PI = 4 * (N_cir / N_sq) MsgBox "After " & tries & " iterations. " & vbCrLf & "PI = " & PI & vbCrLf & vbCrLf & start & vbCrLf & Right(Now, 9), vbOKOnly, "PI calculation result" End Sub
C# ************* -
Hello, I was looking at monte-carlo techniques and found a description of a method for estimating the number PI. I was in work at the time and so i opened Excel wrote it out in VBA and played around trying to see how many iterations it took to get a decent figure. This made me think of preformance issues when making a large number of iterations. At home now i've written the same thing out in C++ and C# and have compared the excecution time and was suprised to find that C# gave a slightly quicker time than C++. I always thought that with the bulk of .net C# would be slower than C++. Here are the approx times(seconds) for 1,000,000,000 iterations. C++ 232 VBA 2122 C# 201 Now i come to my point. Is it possible for you to look at the code below(possibly run it) and see if i have made a fair comparison. I haven't researched the best methods for doing this i've just written it in the first way that worked. I am book-self-taught so i am very likely making serious mistakes so if you see any glaring miatakes please point them out. I'm also interested in finding out how you would really go about testing the efficiency of algorithms if anyone has any advice. Thanks a lot; Sorry this is going to make this a long post: VBA ***********************
Private Const rad As Double = 1 'The radius of the circle. Private Const tries As Double = 1000000000 'The number of iterations Dim N_cir As Double 'The number of hits inside the circle(1/4 circle) Dim N_sq As Double 'The number of hits inside the square. Dim PI As Double 'The final value of PI calculated. Dim i As Double 'A counter Dim x As Double 'X-co-ordinate Dim y As Double 'Y-co-ordinate 'To assign a random number to the x and y co-ordinated Private Function Rand(max As Double) Rand = max * Rnd() End Function Sub FindPI() Dim start As String i = 0 start = Right(Now, 9) Do While i < tries x = Rand(rad) 'Assign random numbers to the y = Rand(rad) 'x and y co-ordinates 'If the random point falls in the circle then 'increment the number of hits in the circle. If ((x * x) + (y * y)) <= rad * rad Then N_cir = N_cir + 1 End If 'The hits will always fall in the square N_sq = N_sq + 1 i = i + 1 Loop PI = 4 * (N_cir / N_sq) MsgBox "After " & tries & " iterations. " & vbCrLf & "PI = " & PI & vbCrLf & vbCrLf & start & vbCrLf & Right(Now, 9), vbOKOnly, "PI calculation result" End Sub
C# *************As a large amount of this time is spent in hte random number generator, this probably says that C# has a more efficient random number generator that your C++ one:
Ylno wrote:
//A function to provide random numbers. double Rnd(int max){ //Will return numbers 0 to max return (max*double(rand())/RAND_MAX);
particularly given that you pass a double as an int parameter - needing conversion every time. Did you optimize the C++ code?
Peter "Until the invention of the computer, the machine gun was the device that enabled humans to make the most mistakes in the smallest amount of time."
-
As a large amount of this time is spent in hte random number generator, this probably says that C# has a more efficient random number generator that your C++ one:
Ylno wrote:
//A function to provide random numbers. double Rnd(int max){ //Will return numbers 0 to max return (max*double(rand())/RAND_MAX);
particularly given that you pass a double as an int parameter - needing conversion every time. Did you optimize the C++ code?
Peter "Until the invention of the computer, the machine gun was the device that enabled humans to make the most mistakes in the smallest amount of time."
-
Hi thanks for the reply "Did you optimize the C++ code? " - why do you mean? I realised that i was passing the wrong argument but i didn't think that the conversion would take any time. Thanks for the pointer
Ylno wrote:
"Did you optimize the C++ code? " - why do you mean?
Did you do your test under debug or release build? Debug build has a lot of overhead. For the release build, what were the optimization settings. -- modified at 23:15 Tuesday 28th August, 2007 on my machine: C++ debug: 277 sec C++ release: 122 sec
Peter "Until the invention of the computer, the machine gun was the device that enabled humans to make the most mistakes in the smallest amount of time."