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