Recently, I've been running some tests with C++ and VB.net to compare execution speeds. In the last thread I posted, I talked about how I had encountered the fact that C++ was executing just as fast as VB, but got that issue resolved. Now I'm hitting my head against another wall:
I had made a DLL for VB.net to test out this theory and compare in just one program side by side execution time of identical VB.net and C++ code. But the interesting thing? VB.net's execution time improved such that it was now exactly identical to the execution time of C++. Spending some time with the problem, I discovered that the "Target CPU" option in advanced compile options in Visual Studio 2008 was the culprit!
Since I'm running 64 bit Windows 7, I figured making the target CPU x64 would yield the best execution time. Wrong. Here are the results in execution time of a Form application for VB.net, calculating all the prime numbers up to 10,000,000 and getting their sum.
Any CPU: 15.231 seconds
x86: 10.858 seconds
x64: 15.236 seconds
Below is the code I'm using, feel free to test it yourself:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim watch As New Stopwatch
watch.Start()
Dim maxVal As Long = 10000000
Dim sumOfPrimes As Long = 0
For i As Integer = 2 To maxVal
If (isPrime(i) = True) Then
sumOfPrimes += i
End If
Next
watch.Stop()
Console.WriteLine(watch.ElapsedMilliseconds)
Console.WriteLine("The sum of all the prime numbers below " & maxVal & " is " & sumOfPrimes)
End Sub
Function isPrime(ByVal NumToCheck As Integer) As Boolean
For i As Integer = 2 To (Math.Sqrt(CDbl(NumToCheck)))
If (NumToCheck Mod i = 0) Then
Return False
End If
Next
Return True
End Function
End Class
Any ideas why selecting the target CPU as 32 bit when I'm running 64 bit would cause a performance increase? Any help with this problem would be much appreciated. Thanks!