Recent

Author Topic: The fastest integer type?  (Read 4400 times)

Zoran

  • Hero Member
  • *****
  • Posts: 1829
    • http://wiki.lazarus.freepascal.org/User:Zoran
The fastest integer type?
« on: August 09, 2019, 08:26:18 am »
I want to choose the integer on which calculations will perform fastest.

I thought that PtrInt would always be the best choice.
So, as a start, I wrote this:
Code: Pascal  [Select][+][-]
  1. type
  2.  
  3. // fastest integer, but at least 32-bit:
  4.   IntFast32 = {$if SizeOf(Pointer) <= 4}Int32{$else}PtrInt{$endif};
  5.   UIntFast32 = {$if SizeOf(Pointer) <= 4}UInt32{$else}PtrUInt{$endif};
  6.  
  7. // fastest, at least 16-bit:
  8.   IntFast16 = {$if SizeOf(Pointer) <= 2}Int16{$else}IntFast32{$endif};
  9.   UIntFast16 = {$if SizeOf(Pointer) <= 2}UInt16{$else}UIntFast32{$endif};
  10.  

However, I am not sure any more, as I read somewhere that on 64-bit CPU's, 32-bit integer is still the fastest.



lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: The fastest integer type?
« Reply #1 on: August 09, 2019, 08:34:00 am »
Can't you test it? A loop doing some realtively simple calculation should do the trick.

If you want the test to be more thorough you could add a call to some function or proc passing the integer by value. That would test the speed of using that integer with the stack and, in most cases, prevent the test being "tainted" by caching in the CPU.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

Thaddy

  • Hero Member
  • *****
  • Posts: 14213
  • Probably until I exterminate Putin.
Re: The fastest integer type?
« Reply #2 on: August 09, 2019, 09:12:12 am »
The fasted integer type is the native type for the CPU. That's all there is to it.
On Intel it may be slightly different, but access vs calculations is something to keep in mind.
On Intel, the micro code still has to mask out the upper part.
« Last Edit: August 09, 2019, 09:16:00 am by Thaddy »
Specialize a type, not a var.

PascalDragon

  • Hero Member
  • *****
  • Posts: 5446
  • Compiler Developer
Re: The fastest integer type?
« Reply #3 on: August 09, 2019, 09:14:31 am »
I want to choose the integer on which calculations will perform fastest.

I thought that PtrInt would always be the best choice.
So, as a start, I wrote this:
Code: Pascal  [Select][+][-]
  1. type
  2.  
  3. // fastest integer, but at least 32-bit:
  4.   IntFast32 = {$if SizeOf(Pointer) <= 4}Int32{$else}PtrInt{$endif};
  5.   UIntFast32 = {$if SizeOf(Pointer) <= 4}UInt32{$else}PtrUInt{$endif};
  6.  
  7. // fastest, at least 16-bit:
  8.   IntFast16 = {$if SizeOf(Pointer) <= 2}Int16{$else}IntFast32{$endif};
  9.   UIntFast16 = {$if SizeOf(Pointer) <= 2}UInt16{$else}UIntFast32{$endif};
  10.  

However, I am not sure any more, as I read somewhere that on 64-bit CPU's, 32-bit integer is still the fastest.
Please note that your check for the size of Pointer in the 16-bit case is not entirely correct as the size depends on the selected memory model (on i8086 obviously) and thus you might have a pointer type of size 4 on a 16-bit system (e.g. in Huge memory model). Thus your IntFast16 would be IntFast32 there which would not be the fastest type.

wp

  • Hero Member
  • *****
  • Posts: 11858
Re: The fastest integer type?
« Reply #4 on: August 09, 2019, 10:17:08 am »
I want to choose the integer on which calculations will perform fastest.
I think this is almost useless micro-optimization. There's much more to gain in speed when you focus on the algorithm and the data structures used by the calculation, or on simple loop errors such as calculating something in a loop which does not change there.

LemonParty

  • Jr. Member
  • **
  • Posts: 58
Re: The fastest integer type?
« Reply #5 on: August 09, 2019, 10:20:18 am »
The fasted integer type is the native type for the CPU. That's all there is to it.
No.

If we talking about performance then the fastest integer is the smallest.
8 bit integer needs 4 times less cache space then 32 bit integer.
And L1 cache is ~80 times faster then memory.

Zoran

  • Hero Member
  • *****
  • Posts: 1829
    • http://wiki.lazarus.freepascal.org/User:Zoran
Re: The fastest integer type?
« Reply #6 on: August 09, 2019, 10:23:09 am »
Thank you all.


Please note that your check for the size of Pointer in the 16-bit case is not entirely correct as the size depends on the selected memory model (on i8086 obviously) and thus you might have a pointer type of size 4 on a 16-bit system (e.g. in Huge memory model). Thus your IntFast16 would be IntFast32 there which would not be the fastest type.

Would it be better then to ask SizeOf(SizeInt) in condition, and then to use SizeInt instead of PtrInt?

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: The fastest integer type?
« Reply #7 on: August 09, 2019, 11:22:45 am »
8 bit integer needs 4 times less cache space then 32 bit integer.

That's not exactly true. The processor will cache the whole 32(64) bit data in the bus and there will be further speed-downs in the internal pipelines caused by the need to discard the unused bytes on the data before passing it on.

I would consider all that negligible (we're talking fractions of nanoseconds here) but since we are taliking about absolute speed ...
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

Thaddy

  • Hero Member
  • *****
  • Posts: 14213
  • Probably until I exterminate Putin.
Re: The fastest integer type?
« Reply #8 on: August 09, 2019, 11:29:24 am »
The fasted integer type is the native type for the CPU. That's all there is to it.
No.

If we talking about performance then the fastest integer is the smallest.
8 bit integer needs 4 times less cache space then 32 bit integer.
And L1 cache is ~80 times faster then memory.

That is simply not true.  The caches - in fact all of the CPU caches - are aligned to the native (register) type. Even on modern processors that allow unaligned data.
Caches are speed efficient, not space efficient.
« Last Edit: August 09, 2019, 06:52:49 pm by Thaddy »
Specialize a type, not a var.

jamie

  • Hero Member
  • *****
  • Posts: 6091
Re: The fastest integer type?
« Reply #9 on: August 09, 2019, 04:28:39 pm »
The fastest integer type would be that on the CPU register size because Fpc will do a lot of conversions between them.

 Since FPC does a lot of upshift conversion in code it's best to use the natural number size.

 this is true for 32 bit targets however, in the 64bit target its upshifted to 64 bit regs most of the
time when ever any processing is done, so there is a trade off there.

 I suppose if you used all 64int in a 64bit target it would scream! :D
The only true wisdom is knowing you know nothing

LemonParty

  • Jr. Member
  • **
  • Posts: 58
Re: The fastest integer type?
« Reply #10 on: August 10, 2019, 12:06:58 pm »
I put 50$, that you can't write a faster code of processing buffer of integers than mine.
I mean two buffers of the same amount of elements but one contain 8 bit int and another 32 bit integers.
(arch x86)

Thaddy

  • Hero Member
  • *****
  • Posts: 14213
  • Probably until I exterminate Putin.
Re: The fastest integer type?
« Reply #11 on: August 10, 2019, 12:41:09 pm »
send me the $50 in advance plz.
And plz specify what size of integer you mean and what architecture of x86: 8, 16, 32, 64, 128,256? (Latter two for AVXx/SSEx)
« Last Edit: August 10, 2019, 12:50:26 pm by Thaddy »
Specialize a type, not a var.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11383
  • FPC developer.
Re: The fastest integer type?
« Reply #12 on: August 10, 2019, 12:41:38 pm »
I put 50$, that you can't write a faster code of processing buffer of integers than mine.
I mean two buffers of the same amount of elements but one contain 8 bit int and another 32 bit integers.
(arch x86)

The code already is avx2 ?

Thaddy

  • Hero Member
  • *****
  • Posts: 14213
  • Probably until I exterminate Putin.
Re: The fastest integer type?
« Reply #13 on: August 10, 2019, 12:44:33 pm »
I put 50$, that you can't write a faster code of processing buffer of integers  than mine.
I mean two buffers of the same amount of elements but one contain 8 bit int and another 32 bit integers.
(arch x86)

The code already is avx2 ?

You can also send the $50 to Marco    :D :)
Specialize a type, not a var.

LemonParty

  • Jr. Member
  • **
  • Posts: 58
Re: The fastest integer type?
« Reply #14 on: August 10, 2019, 01:07:41 pm »
The code already is avx2 ?
It's enough SSE2 to beat AVX2 that working with Int32.

 

TinyPortal © 2005-2018