Skip to content

Improved CompareDWord: generic, i386, x86-64.

Rika requested to merge runewalsh/source:comparedword into main

!373 (merged) but for DWords. >_<

Realistic benchmark: in the example from !369 (merged), after replacing CompareByte(n) with CompareDWord(n div 4) and trying current and proposed CompareDWords, I see 0% :( speedup on i386 (disappointing but consistent with the fact that for typical n it uses, ≤128 bytes, they work the same way) and 10% speedup on x86-64.

Unrealistic benchmark: CompareDWordBenchmark.pas.

My results:

i386                                            x86-64
CompareDWordGeneric: 272 b                      CompareDWordGeneric: 304 b
CompareDWordGenericV2: 80 b                     CompareDWordGenericV2: 240 b

Different byte #3 of 4                          Different byte #3 of 4
System.CompareDWord:       2.1 ns/call          System.CompareDWord:       2.9 ns/call
CompareDWordGeneric:       3.4 ns/call          CompareDWordGeneric:       3.2 ns/call
CompareDWordGenericV2:     2.3 ns/call          CompareDWordGenericV2:     2.3 ns/call
CompareDWordAsm:           1.6 ns/call          CompareDWordAsm:           1.9 ns/call

Different byte #15 of 16                        Different byte #15 of 16
System.CompareDWord:       4.2 ns/call          System.CompareDWord:       5.8 ns/call
CompareDWordGeneric:       6.4 ns/call          CompareDWordGeneric:       6.8 ns/call
CompareDWordGenericV2:     4.2 ns/call          CompareDWordGenericV2:     3.1 ns/call
CompareDWordAsm:           3.0 ns/call          CompareDWordAsm:           2.1 ns/call

Different byte #27 of 28                        Different byte #27 of 28
System.CompareDWord:       4.5 ns/call          System.CompareDWord:       7.4 ns/call
CompareDWordGeneric:       11 ns/call           CompareDWordGeneric:       7.8 ns/call
CompareDWordGenericV2:     4.8 ns/call          CompareDWordGenericV2:     4.3 ns/call
CompareDWordAsm:           4.6 ns/call          CompareDWordAsm:           3.0 ns/call

Different byte #31 of 32                        Different byte #31 of 32
System.CompareDWord:       4.9 ns/call          System.CompareDWord:       7.9 ns/call
CompareDWordGeneric:       12 ns/call           CompareDWordGeneric:       8.2 ns/call
CompareDWordGenericV2:     5.7 ns/call          CompareDWordGenericV2:     5.4 ns/call
CompareDWordAsm:           5.0 ns/call          CompareDWordAsm:           2.6 ns/call

Different byte #35 of 36                        Different byte #35 of 36
System.CompareDWord:       5.6 ns/call          System.CompareDWord:       8.3 ns/call
CompareDWordGeneric:       13 ns/call           CompareDWordGeneric:       8.7 ns/call
CompareDWordGenericV2:     5.9 ns/call          CompareDWordGenericV2:     6.2 ns/call
CompareDWordAsm:           5.5 ns/call          CompareDWordAsm:           2.7 ns/call

Different byte #95 of 96                        Different byte #95 of 96
System.CompareDWord:       11 ns/call           System.CompareDWord:       14 ns/call
CompareDWordGeneric:       26 ns/call           CompareDWordGeneric:       13 ns/call
CompareDWordGenericV2:     10 ns/call           CompareDWordGenericV2:     9.2 ns/call
CompareDWordAsm:           9.4 ns/call          CompareDWordAsm:           4.9 ns/call

Different byte #1 of 200                        Different byte #1 of 200
System.CompareDWord:       16 ns/call           System.CompareDWord:       4.9 ns/call
CompareDWordGeneric:       4.7 ns/call          CompareDWordGeneric:       4.6 ns/call
CompareDWordGenericV2:     2.3 ns/call          CompareDWordGenericV2:     3.0 ns/call
CompareDWordAsm:           1.6 ns/call          CompareDWordAsm:           2.1 ns/call

Different byte #199 of 200                      Different byte #199 of 200
System.CompareDWord:       40 ns/call           System.CompareDWord:       20 ns/call
CompareDWordGeneric:       56 ns/call           CompareDWordGeneric:       19 ns/call
CompareDWordGenericV2:     19 ns/call           CompareDWordGenericV2:     15 ns/call
CompareDWordAsm:           17 ns/call           CompareDWordAsm:           9.2 ns/call

Different byte #1999 of 2000                    Different byte #1999 of 2000
System.CompareDWord:       262 ns/call          System.CompareDWord:       193 ns/call
CompareDWordGeneric:       458 ns/call          CompareDWordGeneric:       136 ns/call  ← should be same as System.CompareDWord,
CompareDWordGenericV2:     159 ns/call          CompareDWordGenericV2:     126 ns/call    ignore this...
CompareDWordAsm:           162 ns/call          CompareDWordAsm:           75 ns/call
Edited by Rika

Merge request reports