Skip to main content
edited title
Link
Henners2002
  • 198
  • 1
  • 10

Code Optimisation: Fastest way to multiply every number in an array by 2

added 54 characters in body
Source Link
Henners2002
  • 198
  • 1
  • 10

I have included a the Benchmarks of the 11 ways of achieving this task where the version above is 'SIMD_no_heap_allocation'. Please see the thread for the code of the other versions and to see how far we have come.

I have included a the Benchmarks of the 11 ways of achieving this task where the version above is 'SIMD_no_heap_allocation'. Please see the thread for the code.

I have included a the Benchmarks of the 11 ways of achieving this task where the version above is 'SIMD_no_heap_allocation'. Please see the thread for the code of the other versions and to see how far we have come.

added 1125 characters in body
Source Link
Henners2002
  • 198
  • 1
  • 10

I'm trying to find the fastest way to multiply every element in an array by a given number, in this case 2. So far I have written a SIMD instruction that performs at 6533 ns multiplying every number in an array of size 1000 by 2.

The techniques used so far are: SIMD instructions, MemoryMarshal method for the loop, Spans, Vector256 Unsafe loading and storing, BitHacks for the multiplication and calculating upper limit of SIMD instructions.

I have included a the Benchmarks of fivethe 11 ways of achieving this task where the version above is 'SIMD_no_heap_allocation'. Please see the thread for the code.

| Method                              |                  | Mean         | Error |StdDev     | StdDev     | Gen0   | Gen1   | Allocated |
|------------------------------------------------------ |-------------:|-----------:|-----------:|-------:|-------:|----------:|
| PLINQ                                                 | 22,086.67 ns | 301.232 ns | 235.182 ns | 2.9907 | 0.0305 |   17968 B |
| LINQ                                                  |  1,747674.413 ns | 16  3.52968 ns | 12  3.90518 ns | 0.6485 |      - |    4072 B |
| for_loop                                              |   613 597.042 ns |  4 2.35075 ns |  4 1.07839 ns | 0.6409 |      - |    4024 B |
| memory_marshall_method_bit_hacked                     |    373.40 ns |   0.442 ns |   0.392 ns |      - |      - |         - |
| SIMD                                                  |   221 208.113 ns |  4 0.39870 ns |  4 0.31727 ns | 0.6413 |      - |    4024 B |
| SIMD_no_heap_allocation                               |   217 209.512 ns |   0.78448 ns |   0.69374 ns |      - |      - |         - |
| SIMD_no_heap_allocation_in_parallelSIMD_no_heap_allocation_bit_hacked                    | 5,969   129.227 ns | 68  0.29766 ns |   0.679 ns |      - |      - |         - |
| SIMD_memory_marshall_method                           |     59.86 ns |   0.942 ns |   0.925 ns |      - |      - |         - |
| SIMD_Vector256_Memory_marshal                         |     58.63 ns |   0.88171 ns |   0.2899160 ns |    1832 B - |      - |         - |
| SIMD_Vector256_Memory_Marshal_unrolled                |     37.67 ns |   0.114 ns |   0.101 ns |      - |      - |         - |
| SIMD_Vector256_Memory_Marshal_unrolled_quicker_bounds |     33.85 ns |   0.110 ns |   0.103 ns |      - |      - |         - |

I'm trying to find the fastest way to multiply every element in an array by a given number, in this case 2. So far I have written a SIMD instruction that performs at 65 ns multiplying every number in an array of size 1000 by 2.

The techniques used so far are: SIMD instructions, MemoryMarshal method for the loop, Spans, Vector256 Unsafe loading, BitHacks for the multiplication and calculating upper limit of SIMD instructions.

I have included a the Benchmarks of five ways of achieving this task where the version above is 'SIMD_no_heap_allocation'

| Method                              |  Mean   | Error |StdDev |  Gen0  | Allocated |
|-------------------------------------|--------:|------:|------:|-------:|----------:|
| LINQ                                | 1,747.4 | 16.52 | 12.90 | 0.6485 |    4072 B |
| for_loop                            |   613.0 |  4.35 |  4.07 | 0.6409 |    4024 B |
| SIMD                                |   221.1 |  4.39 |  4.31 | 0.6413 |    4024 B |
| SIMD_no_heap_allocation             |   217.5 |  0.78 |  0.69 |      - |         - |
| SIMD_no_heap_allocation_in_parallel | 5,969.2 | 68.29 | 63.88 | 0.2899 |    1832 B |

I'm trying to find the fastest way to multiply every element in an array by a given number, in this case 2. So far I have written a SIMD instruction that performs at 33 ns multiplying every number in an array of size 1000 by 2.

The techniques used so far are: SIMD instructions, MemoryMarshal method for the loop, Spans, Vector256 Unsafe loading and storing, BitHacks for the multiplication and calculating upper limit of SIMD instructions.

I have included a the Benchmarks of the 11 ways of achieving this task where the version above is 'SIMD_no_heap_allocation'. Please see the thread for the code.

| Method                                                | Mean         | Error      | StdDev     | Gen0   | Gen1   | Allocated |
|------------------------------------------------------ |-------------:|-----------:|-----------:|-------:|-------:|----------:|
| PLINQ                                                 | 22,086.67 ns | 301.232 ns | 235.182 ns | 2.9907 | 0.0305 |   17968 B |
| LINQ                                                  |  1,674.13 ns |   3.968 ns |   3.518 ns | 0.6485 |      - |    4072 B |
| for_loop                                              |    597.42 ns |   2.075 ns |   1.839 ns | 0.6409 |      - |    4024 B |
| memory_marshall_method_bit_hacked                     |    373.40 ns |   0.442 ns |   0.392 ns |      - |      - |         - |
| SIMD                                                  |    208.13 ns |   0.870 ns |   0.727 ns | 0.6413 |      - |    4024 B |
| SIMD_no_heap_allocation                               |    209.12 ns |   0.448 ns |   0.374 ns |      - |      - |         - |
| SIMD_no_heap_allocation_bit_hacked                    |    129.27 ns |   0.766 ns |   0.679 ns |      - |      - |         - |
| SIMD_memory_marshall_method                           |     59.86 ns |   0.942 ns |   0.925 ns |      - |      - |         - |
| SIMD_Vector256_Memory_marshal                         |     58.63 ns |   0.171 ns |   0.160 ns |      - |      - |         - |
| SIMD_Vector256_Memory_Marshal_unrolled                |     37.67 ns |   0.114 ns |   0.101 ns |      - |      - |         - |
| SIMD_Vector256_Memory_Marshal_unrolled_quicker_bounds |     33.85 ns |   0.110 ns |   0.103 ns |      - |      - |         - |
update image description
Source Link
Loading
replace rendered markup with the real thing - as a preformatted block. Interpreted, it looks - spaceous
Source Link
greybeard
  • 6.6k
  • 3
  • 20
  • 52
Loading
edited tags
Link
Henners2002
  • 198
  • 1
  • 10
Loading
edited title
Source Link
Henners2002
  • 198
  • 1
  • 10
Loading
added 127 characters in body
Source Link
Henners2002
  • 198
  • 1
  • 10
Loading
added 380 characters in body
Source Link
Henners2002
  • 198
  • 1
  • 10
Loading
added 171 characters in body
Source Link
Henners2002
  • 198
  • 1
  • 10
Loading
deleted 37 characters in body
Source Link
Peter Csala
  • 9.9k
  • 1
  • 15
  • 33
Loading
Source Link
Henners2002
  • 198
  • 1
  • 10
Loading