|
-
Aug 3rd, 2009, 07:08 PM
#1
[RESOLVED] Make this faster
My friend and I are comparing VB's speed to C++, and so far his is a lot faster (about 10 x's).
Maybe some speed guru could help make this even faster using some APIs I probably don't know about or other techniques? It's basically math.
Anyway, here's my code, followed by his C++ code.
Code:
Option Explicit
Private Const MILLION As Long = 1000001
Private Type Vector
x As Long
y As Long
z As Long
End Type
Private Type LARGE_INTEGER
LowPart As Long
HighPart As Long
End Type
Private Declare Function QueryPerformanceCounter Lib "kernel32" (lpPerformanceCount As LARGE_INTEGER) As Long
Private Declare Function QueryPerformanceFrequency Lib "kernel32" (lpFrequency As LARGE_INTEGER) As Long
Private Declare Sub CopyMemory Lib "kernel32.dll" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Private udtStart As LARGE_INTEGER, udtEnd As LARGE_INTEGER
Private curStart As Currency, curEnd As Currency
Private udtFrequency As LARGE_INTEGER, curFrequency As Currency
Private Sub Vector_Add(ByRef TheVector As Vector, ByVal Factor As Long)
With TheVector
.x = .x + Factor
.y = .y + Factor
.z = .z + Factor
End With
End Sub
Private Sub Vector_Multiply(ByRef TheVector As Vector, ByVal Factor As Long)
With TheVector
.x = .x * Factor
.y = .y * Factor
.z = .z * Factor
End With
End Sub
Private Sub Vector_Divide(ByRef TheVector As Vector, ByVal Factor As Long)
With TheVector
.x = .x / Factor
.y = .y / Factor
.z = .z / Factor
End With
End Sub
Private Function LargeIntToCurrency(ByRef LargeInteger As LARGE_INTEGER) As Currency
CopyMemory LargeIntToCurrency, LargeInteger, LenB(LargeInteger)
LargeIntToCurrency = LargeIntToCurrency * 10000
End Function
Private Sub Form_Load()
If QueryPerformanceFrequency(udtFrequency) = 0 Then
MsgBox "QPC not available on your piece of **** computer!", vbExclamation
Unload Me
End If
curFrequency = LargeIntToCurrency(udtFrequency)
Dim v1 As Vector, v2 As Vector, v3 As Vector
With v1
.x = 1
.y = 2
.z = 3
End With
With v2
.x = 100
.y = 200
.z = 300
End With
With v3
.x = 40
.y = 50
.z = 60
End With
Dim tmp1 As Vector, tmp2 As Vector
Dim tmp3 As Vector, tmp As Vector
tmp = v1
'tmp += 1
Vector_Add tmp, 1
If tmp.x = 2 And tmp.y = 3 And tmp.z = 4 Then
MsgBox "Test1: Ok!", vbInformation
End If
tmp = v2
'tmp *= 2
Vector_Multiply tmp, 2
If tmp.x = 200 And tmp.y = 400 And tmp.z = 600 Then
MsgBox "Test2: Ok!", vbInformation
End If
tmp = v3
Vector_Divide tmp, 10
If tmp.x = 4 And tmp.y = 5 And tmp.z = 6 Then
MsgBox "Test3: Ok!", vbInformation
End If
'* Timed code begins here *
'TIME_BEGIN:
QueryPerformanceCounter udtStart
Dim i As Long
For i = 1 To MILLION
'Vector 1
'CopyMemory tmp, v1, 12
tmp = v1
'Add, multiply, divide
With tmp
.x = .x + i
.y = .y + i
.z = .z + i
.x = .x * i
.y = .y * i
.z = .z * i
.x = .x / i
.y = .y / i
.z = .z / i
End With
'Vector 2
'CopyMemory tmp, v2, 12
tmp = v2
With tmp
.x = .x + i
.y = .y + i
.z = .z + i
.x = .x * i
.y = .y * i
.z = .z * i
.x = .x / i
.y = .y / i
.z = .z / i
End With
'Vector 3
'CopyMemory tmp, v3, 12
tmp = v3
With tmp
.x = .x + i
.y = .y + i
.z = .z + i
.x = .x * i
.y = .y * i
.z = .z * i
.x = .x / i
.y = .y / i
.z = .z / i
End With
Next i
'TIME_END:
QueryPerformanceCounter udtEnd
curStart = LargeIntToCurrency(udtStart)
curEnd = LargeIntToCurrency(udtEnd)
'((end - start) / freq) * 1000)
MsgBox ((curEnd - curStart) / curFrequency) * 1000, vbInformation
End Sub
C++ code:
Code:
#include <stdio.h>
#include <windows.h>
#pragma comment(lib, "kernel32")
#define TIME_BEGIN double freq, start, end; \
QueryPerformanceFrequency((LARGE_INTEGER *)&freq); \
QueryPerformanceCounter((LARGE_INTEGER *)&start);
#define TIME_END QueryPerformanceCounter((LARGE_INTEGER *)&end); \
printf("Timer: %f ms\n", ((end - start) / freq) * 1000);
struct vector
{
double x, y, z;
void operator += (long f)
{
x += f;
y += f;
z += f;
}
void operator *= (long f)
{
x *= f;
y *= f;
z *= f;
}
void operator /= (long d)
{
x /= d;
y /= d;
z /= d;
}
};
#define MILLION 1000000
vector v1 = { 1.0, 2.0, 3.0 };
vector v2 = { 100.0, 200.0, 300.0 };
vector v3 = { 40.0, 50.0, 60.0 };
vector tmp1;
vector tmp2;
vector tmp3;
vector tmp;
int main()
{
tmp = v1;
tmp += 1;
if (tmp.x == 2.0 && tmp.y == 3.0 && tmp.z == 4.0)
printf("test1: Ok!\n");
tmp = v2;
tmp *= 2;
if (tmp.x == 200.0 && tmp.y == 400.0 && tmp.z == 600.0)
printf("test2: Ok!\n");
tmp = v3;
tmp /= 10;
if (tmp.x == 4.0 && tmp.y == 5.0 && tmp.z == 6.0)
printf("test3: Ok!\n");
TIME_BEGIN;
for (long i = 0; i < MILLION; ++i)
{
tmp1 = v1;
tmp1 += i;
tmp1 *= i;
tmp1 /= i;
tmp2 = v2;
tmp2 += i;
tmp2 *= i;
tmp2 /= i;
tmp3 = v3;
tmp3 += i;
tmp3 *= i;
tmp3 /= i;
}
TIME_END;
while (true);
return 0;
}
Edit: His code only takes about 5ms compared to mine, which takes about 500ms.
Last edited by DigiRev; Aug 3rd, 2009 at 07:37 PM.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|