C
Chris Dollin
Nick said:(e-mail address removed) wrote:
(fx:snip)
is it september already?
For the last ten years.
Nick said:(e-mail address removed) wrote:
(fx:snip)
is it september already?
Hello Old Wolf and Richard, could you write some graceful versions of
palindromic function
Or if toupper and tolower are implemented as efficient macros your
method might be slower.
Michael said:If it does, it only saves it in the case where the first half of the
or-expression is true, ie if *right points to an uppercase letter
which is the same letter as pointed to by *left. In normal English
prose, uppercase letters are relatively infrequent, so the second
call would only be skipped in rather uncommon circumstances when
operating on normal input.
lovecreatesbeauty said:/*palindrome.c*/
#include <stddef.h>
#include <string.h>
#include <ctype.h>
/*determine a sentence if it is a palindrome. s: the string to be
determined. return 1 for identifying a successful palindrome, 0 for
not. The code is mainly derived from Robert Gamble's code. - jhl, Jul 2006*/
int palindrome(const char *s)
{
int pln = 1;
const char *left, *right;
if (s != NULL){
left = s;
right = s + strlen(s) - 1;
while (left < right){
if (!isalnum(*left)){
++left;
continue;
}
if (!isalnum(*right)){
--right;
continue;
}
if (toupper(*left) == *right || tolower(*left) == *right){
++left;
--right;
continue;
} else {
pln = 0; /*charaters occur mismatched, it is not palindrome*/
break;
}
}
}
return pln;
}
Yeah! It's more efficient after exchange those two function call, make
it as:
if (tolower(*left) == *right || toupper(*left) == *right){
Michael said:Well, there's a small lesson: when optimizing, concentrate on the
most common cases first, as a general rule.
But this was the least important of my points. As Flash originally
pointed out, on most implementations it's likely that:
if (tolower(*left) == tolower(*right))
is more efficient than either of your variations,
> as well as being
shorter and arguably clearer. (This style of coding may also be less
prone to error, as you only have to get the identifiers "left" and
"right" correct once.)
And *far* more important is the realization that *performance almost
certainly does not matter here*. Prefer instead to concentrate on
correctness and readability.
There is another lesson as well. Optimisation really is for experts
only, because if you are not an expert on the specific implementation it
is quite easy for your supposed optimisation to actually produce code
that is almost certain to be slower (as with lovecreatsbeauty's original
suggestion).
if "Optimisation" is for experts only, nobody could optimise anything.
av said:if "Optimisation" is for experts only, nobody could optimise anything.
First rule of optimisation: Don't Do It.
Second rule of optimisation (for expert only): Don't Do It Yet.
noone become expert if not try to optimize
so if noone of not expert optimize there will be no expert
av said:noone become expert if not try to optimize
so if noone of not expert optimize there will be no expert
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.