Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. IT & Infrastructure
  4. Comparison, VBA, C++ and C#

Comparison, VBA, C++ and C#

Scheduled Pinned Locked Moved IT & Infrastructure
csharpc++testingbeta-testing
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Y Offline
    Y Offline
    Ylno
    wrote on last edited by
    #1

    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# *************

    C 1 Reply Last reply
    0
    • Y Ylno

      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# *************

      C Offline
      C Offline
      cp9876
      wrote on last edited by
      #2

      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."

      Y 1 Reply Last reply
      0
      • C cp9876

        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."

        Y Offline
        Y Offline
        Ylno
        wrote on last edited by
        #3

        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

        C 1 Reply Last reply
        0
        • Y Ylno

          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

          C Offline
          C Offline
          cp9876
          wrote on last edited by
          #4

          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."

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups