digitalmars.D.learn - quick question, probably of little importance...
- WhatMeWorry (11/11) Apr 26 2023 I just need an even/odd functionality. Don't think D has a
- Richard (Rikki) Andrew Cattermole (3/3) Apr 26 2023 Don't forget ``num % 2 == 0``.
- WhatMeWorry (5/9) Apr 26 2023 Thanks. Fastest reply ever! And I believe across the world? I
- H. S. Teoh (8/19) Apr 26 2023 If performance matters, you'd be using an optimizing compiler. And
- Richard (Rikki) Andrew Cattermole (5/15) Apr 26 2023 Indeed, all the way from New Zealand.
- Cecil Ward (54/64) Apr 30 2023 I made a small change, making the retval a bool rather than an
- Cecil Ward (6/8) Apr 30 2023 Correction: I can’t count. There are only two instructions in
I just need an even/odd functionality. Don't think D has a built-in operator. // Found this C code online. int isEven(int num) { return !(num & 1); } // found this in std.functional.unaryFun alias isEven = unaryFun!("(a & 1) == 0"); assert(isEven(2) && !isEven(1)); If interested just in speed, is either one faster?
Apr 26 2023
Don't forget ``num % 2 == 0``. None should matter, pretty much all production compilers within the last 30 years should recognize all forms of this and do the right thing.
Apr 26 2023
On Wednesday, 26 April 2023 at 23:02:07 UTC, Richard (Rikki) Andrew Cattermole wrote:Don't forget ``num % 2 == 0``. None should matter, pretty much all production compilers within the last 30 years should recognize all forms of this and do the right thing.Thanks. Fastest reply ever! And I believe across the world? I suppose my examples required overhead of a function call. So maybe num % 2 == 0 is fastest?
Apr 26 2023
On Wed, Apr 26, 2023 at 11:07:39PM +0000, WhatMeWorry via Digitalmars-d-learn wrote:On Wednesday, 26 April 2023 at 23:02:07 UTC, Richard (Rikki) Andrew Cattermole wrote:If performance matters, you'd be using an optimizing compiler. And unless you're hiding your function implementation behind a .di, almost all optimizing compilers would inline it, so you shouldn't even be able to tell the difference. T -- Without outlines, life would be pointless.Don't forget ``num % 2 == 0``. None should matter, pretty much all production compilers within the last 30 years should recognize all forms of this and do the right thing.Thanks. Fastest reply ever! And I believe across the world? I suppose my examples required overhead of a function call. So maybe num % 2 == 0 is fastest?
Apr 26 2023
On 27/04/2023 11:07 AM, WhatMeWorry wrote:On Wednesday, 26 April 2023 at 23:02:07 UTC, Richard (Rikki) Andrew Cattermole wrote:Indeed, all the way from New Zealand. I wouldn't bother timing this. A compiler will rewrite it and emit whatever instructions it thinks is best for a given cpu target that you select. This is one of those things that was solved 30 years ago :)Don't forget ``num % 2 == 0``. None should matter, pretty much all production compilers within the last 30 years should recognize all forms of this and do the right thing.Thanks. Fastest reply ever! And I believe across the world? I suppose my examples required overhead of a function call. So maybe num % 2 == 0 is fastest?
Apr 26 2023
On Wednesday, 26 April 2023 at 23:07:39 UTC, WhatMeWorry wrote:On Wednesday, 26 April 2023 at 23:02:07 UTC, Richard (Rikki) Andrew Cattermole wrote:I made a small change, making the retval a bool rather than an int. I got slightly better code generation with the int, as it seems that some of the compilers have not yet got all the good tricks they should be using when manipulating bool-typed expressions and also it can be one extra instruction converting values to bool strictly zero or one, not zero or any non-zero value. Here’s the D, enlarged a little so that we can see your routine in action, inlined. Your isEven boils down to two instructions with a seriously optimising compiler. I’ve included the x86-64 machine code generated by the GDC and LDC compilers so you can see how fast it is. GDC made a bit of a dog’s breakfast of my longer routine whereas LDC performed superbly. GDC generated twice as much code, but its excellent instruction scheduler and what looks like an awareness of ILP mean that the two streams of instructions will be carried out in parallel so the two streams will only take three instruction times - ie whatever the total time is for those three instruction in the one stream - not six. bool isEven( int num ) { return ! ( num & 1 ); } bool AreBothEven( int a, int b ) // returns true if both arguments are even { return isEven( a ) && isEven( b ); } === Compiler output:: GDC:: x86-64: -O3 -mcpu=native -frelease bool isEven( int ): mov eax, edi not eax and eax, 1 ret bool AreBothEven( int, int ): mov eax, edi not esi not eax and esi, 1 and eax, 1 cmovne eax, esi ret === Compiler LDC: x86-64: -O3 -mcpu=native -release bool isEven( int ): test dil, 1 sete al ret bool AreBothEven( int, int ): or edi, esi test dil, 1 sete al retDon't forget ``num % 2 == 0``. None should matter, pretty much all production compilers within the last 30 years should recognize all forms of this and do the right thing.Thanks. Fastest reply ever! And I believe across the world? I suppose my examples required overhead of a function call. So maybe num % 2 == 0 is fastest?
Apr 30 2023
On Monday, 1 May 2023 at 03:53:24 UTC, Cecil Ward wrote:On Wednesday, 26 April 2023 at 23:07:39 UTC, WhatMeWorry wrote:Correction: I can’t count. There are only two instructions in parallel with another pair running alongside, not three. The first reg, reg move counts as zero cycles, so the total time is just the sum of the following three instructions’ times, ignoring the other parallel stream.[...]
Apr 30 2023