Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Best way to implement sgn() function in (G)awk?

383 views
Skip to first unread message

Kenny McCormack

unread,
Sep 11, 2015, 5:47:10 PM9/11/15
to
Can this be improved upon:

function sgn(x) { return x < 0 ? -1 : x > 0 }

?
--
Both the leader of the Mormon Church and the leader of the Catholic
church claim infallibility. Is it any surprise that these two orgs
revile each other? Anybody with any sense knows that 80-yr old codgers
are hardly infallible. Some codgers this age do well to find the crapper
in time and remember to zip-up.

Janis Papanagnou

unread,
Sep 11, 2015, 6:01:49 PM9/11/15
to
Am 12.09.2015 um 00:47 schrieb Kenny McCormack:
> Can this be improved upon:
>
> function sgn(x) { return x < 0 ? -1 : x > 0 }
>
> ?

Well, this is a simple enough function, and you saved already
one conditional expression.

Many decades ago (ISTR) I've learned that it's good to avoid
exactly those conditional expressions; they said that they are
expensive. And they said that doing arithmetic is better than
branching.

I don't know whether that's still relevant with current CPUs,
but here's an option:

function sign(x) { return - (x < 0) + (x > 0) }

(I haven't done performance measurements, though.)

Janis

Janis Papanagnou

unread,
Sep 11, 2015, 6:06:26 PM9/11/15
to
Am 12.09.2015 um 01:01 schrieb Janis Papanagnou:
>
> function sign(x) { return - (x < 0) + (x > 0) }

return (x > 0) - (x < 0)

...just to save some more characters of text, and optimize
binary code in case of non-sophisticated parsers. :-)

Janis

Kenny McCormack

unread,
Sep 11, 2015, 6:33:17 PM9/11/15
to
In article <msvj90$vio$1...@speranza.aioe.org>,
I like that. Thanks.

--
b w r w g y b r y b

Janis Papanagnou

unread,
Sep 11, 2015, 6:54:45 PM9/11/15
to
You're welcome. But see where my curiosity lead me to
WRT performance...

With gawk 4.1.3 on Cygwin, a dataset of 2*10^7 values:

Two ternary operators (classic): 20s
One ternary operator (yours): 20s
No ternary operator (mine): 20s

Inlining (no function call): 6s


Janis

Kees Nuyt

unread,
Sep 12, 2015, 10:41:11 AM9/12/15
to
On Sat, 12 Sep 2015 01:54:51 +0300, Janis Papanagnou
<janis_pa...@hotmail.com> wrote:

[...]

> You're welcome. But see where my curiosity lead me to
> WRT performance...
>
> With gawk 4.1.3 on Cygwin, a dataset of 2*10^7 values:
>
> Two ternary operators (classic): 20s
> One ternary operator (yours): 20s
> No ternary operator (mine): 20s
>
> Inlining (no function call): 6s

Lovely. Now we only need a macro facility or a preprocessor :D

--
Regards,
Kees Nuyt

Janis Papanagnou

unread,
Sep 12, 2015, 3:26:50 PM9/12/15
to
The function call overhead is significant. That was the reason why
I once proposed here to have some inline facility in gawk. The
least intrusive method that I thought of was an implicit inlining;
in case of a definition

function sign (x) {
return (x>0) - (x<0)
}

it would be a expanded to a normal function call, and in case of

function sign (x) { return (x>0) - (x<0) }

(i.e. all in one line) it could result in inline expanded calls.

Alas, at the time I proposed it I had not enough spare time to look
whether it was feasible implementing it. (And probably, don't recall,
the maintainers might also not be thinking that to be a good idea?)

I don't much like the idea of a macro preprocessor because of the
known issue with function arguments that have side effects, like ++n.
If need be one could already use the C-preprocessor for that purpose,
I suppose, which supports macro "functions".

Janis

Kaz Kylheku

unread,
Sep 13, 2015, 11:40:15 AM9/13/15
to
I dare say, you seem to be grasping about for the word "compiler".

That macro-like preprocessing gadget that inlines code, eliminates
common subexpressions, culls dead assignments, etc.
0 new messages