» 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!

SinCos instruction vs VB Sin and Cos

With ASM the instruction fSinCos is available. It calculates both Sin and Cos for the same argument.
Often you use Sin and Cos with the same angle (in radians). And this function will perform it one time as quick as VB's separate Sin and Cos.

'//How-to use:
'//Simply Call:
'//Call asmSinCosPointer(sngCos, sngSin)
'//With: sngCos As Single, sngSin As Single
'//The first argument will be used for the calculation!
'//So only use this if sin and cos need to use the same angle (in radians!)
'//You will get the result of cos in the first argument and Sin in the second
'//50% faster than normal Sin+Cos in VB

Public Sub asmSinCosPointer(ByRef sngCosAngle As Single, ByRef sngSinAngle As Single)
'#ASM_START
'.MMX
' push ebp
' mov ebp, esp

' mov eax, DWORD PTR [ebp+8] ;'//First argument, store in eax (= value now!)

' fld dword ptr [eax] ;'//Get value for cos
' fsincos ;'//Execute sin/cos instruction
' fstp dword ptr [eax] ;'//Store cos in eax (1st param pointer!)

' mov eax, DWORD PTR [ebp+12] ;'//Second argument, store in eax (=value now!)
' fstp dword ptr [eax] ;'//Store sin in eax (2nd param pointer!)

' mov esp, ebp ;'//MOV/POP is much faster
' pop ebp ;'//on 486 and Pentium than Leave
' ret 8
'#ASM_END
End Sub

VBSinCos % faster than ASMSinCos VBSinCos (sec) ASMSinCos (sec)
2149.6% 0.002583 0.000115
3530.7% 0.004169 0.000115
2238.9% 0.00247 0.000106
2466.4% 0.002689 0.000105
2419.2% 0.002639 0.000105

VBSinCos % faster than ASMSinCos VBSinCos (sec) ASMSinCos (sec)
2106.3% 0.004956 0.000225
3689.2% 0.008511 0.000225
2556.1% 0.005988 0.000225
1971.6% 0.005191 0.000251
2285.1% 0.005357 0.000225


User contributed notes:

Author: cjb0087 () Date: 11:03 03/03/2004
i gotta get me this!!

Author: Kristian () Date: 03:07 31/07/2004
uhh... this will really speed up my game, thanx!

Author: Tom (hurendo_kun at hotmail dot com) Date: 16:06 08/06/2005
Yikes, that's fast. Anybody know why the VB versions would be so slow?

Author: Steven (steven at uplinklabs dot net) Date: 18:07 15/07/2005
Because it's VB. VB is a *very* high level language, and as such is very slow.

Author: Tom (hurendo_kun at hotmail dot com) Date: 05:09 23/09/2005
Okay, but does it have its own implementation of Sin/Cos, whereas the ASM listing uses special CPU hardware? Because with normal operations, VB delegates to the FPU, and the speed difference is negligible.

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