I just compiled your code on a VAX (VMS) both with and without optimization enabled. In each case, both routines generated identical code.
To me, the ternary operator is most useful when used in a function call argument list or other complex expression where one element needs to change based on some condition. These become much easier and concise to express with a ternary operator than with two separate statements within an if/else construct.
printf("File name: %s\n", (ptr == NULL) ? "<unspecified>" : ptr);
if (ptr == NULL)
s = "<unspecified>";
s = ptr;
printf("File name: %s\n", s);
What it boils down to is that the ternary operator presents an expression and can be used anywhere an expression can be used. if/else is a (compound) statement and is much more restrictive about where it can be used.
The trinary function is handling the variable between registers, whereas ifelse is using a memory location. This is another boost to the trinary operator.
movl %eax, -28(%ebp) # to_return
movl $1, -28(%ebp) # to_return
Doesn't the $1 notation mean that the value 1 is encoded in the instruction itself (immediate value)? You seem to imply that it is referencing a variable somewhere else in memory.
|90% of every Perl application is already written. ⇒|
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.
| & || & |
| < || < |
| > || > |
| [ || [ |
| ] || ] ||