leopardpm wrote:...cuz he IS the King of pointer manipulation!
Not really but I know how to help a compiler :-)
Long time ago, FreeBASIC was a x86 compiler (emitter) only I have done many things with inline assembler
but since FreeBASIC is a real cross compiler and run's on my cute Raspberry PI and Beagle Bone black also
I try to get my best in "BASIC" as a hobby.
The fastest solution is to have addresses or better offsets to memory addresses calculated to compile time not runtime.
I won't lay me out the window but should be hard to get it faster than this version.
Joshy
Code: Select all
'joshy
Function strRev(byref strIn As String) Byref As String
type tString
as ubyte ptr pChars
as integer nChars
as integer nReserved
end type
static as STRING strOut
var pI=cptr(tString ptr,varptr(strIn))
var pO=cptr(tString ptr,varptr(strOut))
var nChars = pI->nChars
if nChars<2 then return strIn
if nChars<>pO->nChars then
pO->pChars=reallocate(pO->pChars,nChars+1)
pO->nChars=nChars
pO->nReserved=nChars+1
pO->pChars[nChars]=0
end if
dim as ubyte ptr pS=@pI->pChars[nChars-1]
dim as ubyte ptr pD=pO->pChars,pE=pD+nChars
#define NOT_A_TRICK(a) pD[a]=pS[-a]
while nChars>64
NOT_A_TRICK(0) : NOT_A_TRICK(1) : NOT_A_TRICK(2) : NOT_A_TRICK(3)
NOT_A_TRICK(4) : NOT_A_TRICK(5) : NOT_A_TRICK(6) : NOT_A_TRICK(7)
NOT_A_TRICK(8) : NOT_A_TRICK(9) : NOT_A_TRICK(10) : NOT_A_TRICK(11)
NOT_A_TRICK(12) : NOT_A_TRICK(13) : NOT_A_TRICK(14) : NOT_A_TRICK(15)
NOT_A_TRICK(16) : NOT_A_TRICK(17) : NOT_A_TRICK(18) : NOT_A_TRICK(19)
NOT_A_TRICK(20) : NOT_A_TRICK(21) : NOT_A_TRICK(22) : NOT_A_TRICK(23)
NOT_A_TRICK(24) : NOT_A_TRICK(25) : NOT_A_TRICK(26) : NOT_A_TRICK(27)
NOT_A_TRICK(28) : NOT_A_TRICK(29) : NOT_A_TRICK(30) : NOT_A_TRICK(31)
NOT_A_TRICK(32) : NOT_A_TRICK(33) : NOT_A_TRICK(34) : NOT_A_TRICK(35)
NOT_A_TRICK(36) : NOT_A_TRICK(37) : NOT_A_TRICK(38) : NOT_A_TRICK(39)
NOT_A_TRICK(40) : NOT_A_TRICK(41) : NOT_A_TRICK(42) : NOT_A_TRICK(43)
NOT_A_TRICK(44) : NOT_A_TRICK(45) : NOT_A_TRICK(46) : NOT_A_TRICK(47)
NOT_A_TRICK(48) : NOT_A_TRICK(49) : NOT_A_TRICK(50) : NOT_A_TRICK(51)
NOT_A_TRICK(52) : NOT_A_TRICK(53) : NOT_A_TRICK(54) : NOT_A_TRICK(55)
NOT_A_TRICK(56) : NOT_A_TRICK(57) : NOT_A_TRICK(58) : NOT_A_TRICK(59)
NOT_A_TRICK(60) : NOT_A_TRICK(61) : NOT_A_TRICK(62) : NOT_A_TRICK(63)
pD+=64:pS-=64:nChars-=64
wend
while nChars>32
NOT_A_TRICK(0) : NOT_A_TRICK(1) : NOT_A_TRICK(2) : NOT_A_TRICK(3)
NOT_A_TRICK(4) : NOT_A_TRICK(5) : NOT_A_TRICK(6) : NOT_A_TRICK(7)
NOT_A_TRICK(8) : NOT_A_TRICK(9) : NOT_A_TRICK(10) : NOT_A_TRICK(11)
NOT_A_TRICK(12) : NOT_A_TRICK(13) : NOT_A_TRICK(14) : NOT_A_TRICK(15)
NOT_A_TRICK(16) : NOT_A_TRICK(17) : NOT_A_TRICK(18) : NOT_A_TRICK(19)
NOT_A_TRICK(20) : NOT_A_TRICK(21) : NOT_A_TRICK(22) : NOT_A_TRICK(23)
NOT_A_TRICK(24) : NOT_A_TRICK(25) : NOT_A_TRICK(26) : NOT_A_TRICK(27)
NOT_A_TRICK(28) : NOT_A_TRICK(29) : NOT_A_TRICK(30) : NOT_A_TRICK(31)
pD+=32:pS-=32:nChars-=32
wend
while nChars>16
NOT_A_TRICK(0) : NOT_A_TRICK(1) : NOT_A_TRICK(2) : NOT_A_TRICK(3)
NOT_A_TRICK(4) : NOT_A_TRICK(5) : NOT_A_TRICK(6) : NOT_A_TRICK(7)
NOT_A_TRICK(8) : NOT_A_TRICK(9) : NOT_A_TRICK(10) : NOT_A_TRICK(11)
NOT_A_TRICK(12) : NOT_A_TRICK(13) : NOT_A_TRICK(14) : NOT_A_TRICK(15)
pD+=16:pS-=16:nChars-=16
wend
while nChars>8
NOT_A_TRICK(0) : NOT_A_TRICK(1) : NOT_A_TRICK(2) : NOT_A_TRICK(3)
NOT_A_TRICK(4) : NOT_A_TRICK(5) : NOT_A_TRICK(6) : NOT_A_TRICK(7)
pD+=8:pS-=8:nChars-=8
wend
while nChars>4
NOT_A_TRICK(0) : NOT_A_TRICK(1) : NOT_A_TRICK(2) : NOT_A_TRICK(3)
pD+=4:pS-=4:nChars-=4
wend
while pD<pE : *pD=*pS : pS-=1 : pD+=1 : wend
return strOut
End Function
32-bit gen gcc no optimization
0.445 Joshy
0.808 dodicat
0.864 MrSwiss
0.957 Munair
64-bit gen gcc no optimization
0.431 Joshy
0.768 dodicat
0.942 Munair
1.045 MrSwiss