Optimize my code =)

Kapps opantm2+spam at gmail.com
Mon Feb 17 15:35:32 PST 2014


On Monday, 17 February 2014 at 11:34:43 UTC, bearophile wrote:
>
>>       foreach (ref T element; this.data) {
>>           element *= scalar;
>>       }
>
> Try to use:
>
>         data[] *= scalar;
>
>
>>       for (auto i = 0; i < this.dim.size; ++i) {
>>           this.data[i] += other.data[i];
>>       }
>
> Try to use:
>
>         data[] += other.data[];
>
>
>>       for (auto i = 0; i < this.dim.size; ++i) {
>>           this.data[i] -= other.data[i];
>>       }
>
> Try to use:
>
>         data[] -= other.data[];
>
>

While it is cleaner, I remember array operations (such as data[] 
-= other.data[]) being significantly slower than doing it 
yourself. I don't know if this has been fixed. Perhaps using -m64 
helps this.


Also, I stress again that if you aren't compiling for 64-bit 
(-m64), it's very likely to give a significant improvement 
(because the optimizer will probably convert some of your 
operations into SIMD operations). Using LDC / GDC would also give 
a significant improvement.


More information about the Digitalmars-d-learn mailing list