» Home » VB Fibre
Site
News
Files

Visual Basic
Strings
Math
General
Properties
Memory
Methods

Search
Testing

Inline ASM-VB
Strings
Math
General
Memory

Search
Using inline ASM
Submit!

Methods vs Inline

About 25% gain here, using Inline code.

Inline code means that instead of calling code in a sub/function to perform something, you put it directly "inline" the code, so no method has to be called.

I've tested this with 3 Vector operations (often/always used in 3D games). Note that this code might not exactly be representative, since I'm replacing 3 methods with just one complete inline piece of code. But you can definetly count on the fact that inline is faster.

Code:
Public Sub TestTwo() Dim Vec1 As Vector, Vec2 As Vector, Vec3 As Vector Dim sngScaleFactor As Single, sngRet As Single With Vec1 .x = 10 .y = 14 .z = 15 End With With Vec2 .x = 15 .y = 14 .z = 16 End With With Vec3 .x = 14 .y = 17 .z = 15 End With Vec3.x = Vec1.x - Vec2.x Vec3.y = Vec1.y - Vec2.y Vec3.z = Vec1.z - Vec2.z sngRet = (Vec1.x * Vec3.x) + (Vec1.y * Vec3.y) + (Vec1.z * Vec3.z) sngScaleFactor = 1 / Sqr((Vec2.x * Vec2.x) + (Vec2.y * Vec2.y) + (Vec2.z * Vec2.z)) ' Vec2.x = Vec2.x * sngScaleFactor Vec2.y = Vec2.y * sngScaleFactor Vec2.z = Vec2.z * sngScaleFactor End Sub Public Sub TestOne() Dim Vec1 As Vector, Vec2 As Vector, Vec3 As Vector Dim sngRet As Single With Vec1 .x = 10 .y = 14 .z = 15 End With With Vec2 .x = 15 .y = 14 .z = 16 End With With Vec3 .x = 14 .y = 17 .z = 15 End With VectorSub Vec1, Vec2, Vec3 sngRet = VectorDot(Vec1, Vec3) VectorNormalize Vec2 End Sub Public Sub VectorSub(a As Vector, b As Vector, result As Vector) '//Result = a - b result.x = a.x - b.x result.y = a.y - b.y result.z = a.z - b.z End Sub Public Function VectorDot(a As Vector, b As Vector) As Single VectorDot = (a.x * b.x) + (a.y * b.y) + (a.z * b.z) End Function Public Sub VectorNormalize(v As Vector) Dim sngScaleFactor As Single sngScaleFactor = 1 / Sqr((v.x * v.x) + (v.y * v.y) + (v.z * v.z)) v.x = v.x * sngScaleFactor v.y = v.y * sngScaleFactor v.z = v.z * sngScaleFactor End Sub

Methods % faster than Inline Methods (sec) Inline (sec)
26.1% 0.246186 0.195249
22.4% 0.245352 0.200503
26.1% 0.246163 0.195158
22% 0.244236 0.200233
29.9% 0.246804 0.190068


User contributed notes:

Author: Tom (hurendo_kun at hotmail dot com) Date: 15:05 31/05/2005
Placing functions inline is not always practical (since VB has no Inline statement like C/C++), but you can Find/Replace them before your final release and squeeze some extra speed out of it.

Add user-note
Author:
E-mail (optional):
Anti spam, please enter 'ok' without quotes:
Comment: