472,958 Members | 2,580 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 software developers and data experts.

Largest Palindrome

HI Everybody,

I 've to submit a program on c.Can any one help me
plz.........The problem is like this::

Write a program which computes the largest palindrome substring of a
string.

Input:
The input is the string whose largest palindrome substring should be
found.

Output:
The output displays the largest palindrome substring.

Sample Input:
Enter the string: Habitat

Sample Output:
The longest palindrome substring of Habitat is: tat

Jul 11 '06 #1
32 11359
ra***************@gmail.com wrote:

[ Snip whole homework assignment written by a teacher who is clearly
better at non-d00d English than his pupils. ]

And your work so far is? Surely you don't expect us to do your homework
for you?

Richard
Jul 11 '06 #2
MQ

Richard Bos wrote:
ra***************@gmail.com wrote:

[ Snip whole homework assignment written by a teacher who is clearly
better at non-d00d English than his pupils. ]

And your work so far is? Surely you don't expect us to do your homework
for you?

Richard
This really annoys me. As if there isn't enough incompetent IT
professionals out there...

Jul 11 '06 #3
<ra***************@gmail.comwrote:

I 've to submit a program on c.Can any one help me
plz.........The problem is like this::

Write a program which computes the largest palindrome substring of a
string.

Input:
The input is the string whose largest palindrome substring should be
found.

Output:
The output displays the largest palindrome substring.

Sample Input:
Enter the string: Habitat

Sample Output:
The longest palindrome substring of Habitat is: tat
Note that the first letter of a palindrome is the same as the last letter.
Proceed from there.
Jul 11 '06 #4
HI Everybody,
>
I 've to submit a program on c.Can any one help me
plz.........The problem is like this::

Write a program which computes the largest palindrome substring of a
string.

Input:
The input is the string whose largest palindrome substring should be
found.

Output:
The output displays the largest palindrome substring.

Sample Input:
Enter the string: Habitat

Sample Output:
The longest palindrome substring of Habitat is: tat

I had this assignment a few years ago, got an A! Here's the exact code copy-
pasted. The code is perfect, fully-portable... just need to hand it in!

#include
"stdio.h"

#include
"string.h"

void main(int)
{
char[64] buffer;

scanf(STRING,buffer);

int * const p;

while
{
p = permute(buffer, buffer + len);
} do( p --p );
printf(p);
}
Hope that helps, always love to help out a fellow student : ) !
--

Frederick Gotham
Jul 11 '06 #5
On 2006-07-11, Frederick Gotham <fg*******@SPAM.comwrote:
I had this assignment a few years ago, got an A! Here's the exact code copy-
pasted. The code is perfect, fully-portable... just need to hand it in!

#include
"stdio.h"

#include
"string.h"

void main(int)
{
char[64] buffer;

scanf(STRING,buffer);

int * const p;

while
{
p = permute(buffer, buffer + len);
} do( p --p );

printf(p);
}

Hope that helps, always love to help out a fellow student : ) !
I laughed out loud at that one. :-)

--
Andrew Poelstra <http://www.wpsoftware.net/projects/>
To email me, use "apoelstra" at the above domain.
"You people hate mathematics." -- James Harris
Jul 11 '06 #6
Andrew Poelstra wrote:
>
On 2006-07-11, Frederick Gotham <fg*******@SPAM.comwrote:
I had this assignment a few years ago, got an A!
Here's the exact code copy-
pasted. The code is perfect, fully-portable...
just need to hand it in!

#include
"stdio.h"

#include
"string.h"

void main(int)
{
char[64] buffer;

scanf(STRING,buffer);

int * const p;

while
{
p = permute(buffer, buffer + len);
} do( p --p );

printf(p);
}

Hope that helps, always love to help out a fellow student : ) !

I laughed out loud at that one. :-)
Nobody is entitled to make any serious decisions
based on "code tested" and "code not tested" labels
on posted code.

Which is why
I'm not a fan of "code tested" and "code not tested" labels.

No criticism of Frederick Gotham is intended here.

--
pete
Jul 11 '06 #7
Andrew Poelstra wrote:
I laughed out loud at that one. :-)
I have written a function to identify a whole as palindromic or not. My
function knows this one is a palindrome:

A man, a plan, a canal - Panama!

To determine whether the following one is a palindrome will be more
difficult than the last one.

abcdefghijklmnopqrstuvwxyz0123456789 A man, a plan, a canal - Panama!

(please do not laugh at the following function:), but comments are
welcome!)

/*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. - 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;
}

/* test */
#include <stdio.h>
int main()
{
printf("%d\n", palindrome("Madam, I'm Adam!"));
printf("%d\n", palindrome("A man, a plan, a canal - Panama!"));
printf("%d\n", palindrome("Able was I, ere I saw Elba."));
printf("%d\n", palindrome("Ana voli Milovana"));
return 0;
}

$ cc -ansi -pedantic -Wall -W palindrome.c
$ ./a.out
1
1
1
1
$

lovecreatesbeauty

Jul 11 '06 #8
lovecreatesbeauty said:
Andrew Poelstra wrote:
>I laughed out loud at that one. :-)

I have written a function to identify a whole as palindromic or not.
Is it that time already? Here we go, then:

#include <stdio.h>
#include <string.h>
#include <ctype.h>

#define p void
#define a sizeof
#define l char
#define i return
#define n int
#define d size_t
#define r if
#define o while
#define m else
#define e for
#define ob putchar
#define fu strlen
#define sc isalpha
#define at tolower
#define ed fgets
#define cp printf
#define ro strchr
#define gr main
#define am stdin
n c(l*s){n k=
0;r(s){d x
=fu (s)
;r(x>1)
{l*t=s,
*u=s+x-
1;k=1;o
(k&&t<u
){r(sc(
*t)){r(
sc(*u))

{r(at(
*t)!=at(
*u) ){k
=0; }m{
++t;--u;}}m{--
u;}}m{++t;}}}}
i k ;}n
gr( p){
l b [01
<<1 <<1

<<1
<<1
<<1
<<1
<<1
<<1
<<1
],*
z,*s=" adeil"
"mnoprst.\n";

n f[]={00,01<<1
<<1,(1<<1<<1<<1
)|(
1<<
1)|
1},
v[]
={0
,(1<<1<<1)|(1<<
1)|1,1<<1<<1<<1
,(1 <<1
<<01 <<1
)|(01 <<1
<<1 )} ,q[
]={ (1 <<1
)&1 ,1 <<1
>>1 ,1 >>1
,(1 << 1<<
1<< 1) |1,
1<< 01<<1

>>1>>1,(1<<1
<<1 )|(
1<< 1>>
1), 1<<
1<< 1,(
1<< 1<<
1)| (01
<<1 )|1
,01 <<1
,(1<<1<<1<<1
)|(1<<01),1
<<1 <<1
<<1 ,(1
<<1 <<1
)|1 <<1
,(1 <<1
)|1,(1<<
1<< 1<<
1)| (01
<<1 <<1
)|1 ,(1
<<1 <<1
<<1)|(1<<
1<< 1)|
(01 <<1
)}; d y
;o( ed(
b,a (b)
,am )){
{z= ro(
b,1 *1*
'\n');}if(

z){ *z=
00;} {cp(
"%s", b);}e
(y=0;1 *y<a f
/a( 1[f ]); y++
)ob (y[f][ s])
;r( !c(b ))e
(y= 0; y<a
v/a 1[v
];y ++)
ob(01*y[v][s]);e
(y=0/1*1/1<<1<<1
<<1
;y<
a q
/a 1[q]
;y++)ob
(y[
q][
s])
;}i 0>>1<<0>>1<<
1*1/(1<<1<<01);}


--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Jul 11 '06 #9
Richard Heathfield said:
Is it that time already? Here we go, then:
<snip>

Not sure what happened to the formatting there. The original, with proper
formatting, can be found here:

http://www.cpax.org.uk/prg/portable/...s/obfusc/pal.c

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Jul 11 '06 #10
lovecreatesbeauty wrote:

<snip>
if (toupper(*left) == *right || tolower(*left) == *right){
I would replace the above with either
if (toupper(*left) == toupper(*right)){
or
if (tolower(*left) == tolower(*right)){

depending on my mood. Also there is the debate about whether left and
right should be pointers to unsigned char or whether you should cast the
dereferenced value to unsigned char. Personally, I would make them
pointers to unsigned char for this function.

<snip>
--
Flash Gordon, living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidelines and intro:
http://clc-wiki.net/wiki/Intro_to_clc
Jul 11 '06 #11
Flash Gordon wrote:
lovecreatesbeauty wrote:
if (toupper(*left) == *right || tolower(*left) == *right){
I would replace the above with either
if (toupper(*left) == toupper(*right)){
or
if (tolower(*left) == tolower(*right)){
I expect this one may save one additional function call.

if (toupper(*left) == *right || tolower(*left) == *right){
depending on my mood. Also there is the debate about whether left and
right should be pointers to unsigned char or whether you should cast the
dereferenced value to unsigned char. Personally, I would make them
pointers to unsigned char for this function.
int palindrome(const char *s)
{
/*...*/
left = s;
right = s + strlen(s) - 1;
/*...*/

left and right is the same type as parameter s's, i.e. char *, and char
may be signed or unsigned, it is undefined. The sign flag does not
matter, does it?

lovecreatesbeauty

Jul 11 '06 #12

Richard Heathfield wrote:
lovecreatesbeauty said:
Andrew Poelstra wrote:
I laughed out loud at that one. :-)
I have written a function to identify a whole as palindromic or not.

Is it that time already? Here we go, then:

#include <stdio.h>
#include <string.h>
#include <ctype.h>

#define p void
#define a sizeof
#define l char
#define i return
#define n int
<snip>

I just meant a whole string (sentence), not a sub-string, not a file.
But you can read your files one by one into a string variable (it is
your duty) then invoke my function (through the interface), it may
works :)

lovecreatesbeauty

Jul 11 '06 #13
lovecreatesbeauty wrote:
Flash Gordon wrote:
>lovecreatesbeauty wrote:
>> if (toupper(*left) == *right || tolower(*left) == *right){
I would replace the above with either
if (toupper(*left) == toupper(*right)){
or
if (tolower(*left) == tolower(*right)){

I expect this one may save one additional function call.

if (toupper(*left) == *right || tolower(*left) == *right){
Or if toupper and tolower are implemented as efficient macros your
method might be slower. I merely expressed my preference, which IMHO
shows the intent better) I did not say your code was wrong.
>depending on my mood. Also there is the debate about whether left and
right should be pointers to unsigned char or whether you should cast the
dereferenced value to unsigned char. Personally, I would make them
pointers to unsigned char for this function.

int palindrome(const char *s)
{
/*...*/
left = s;
right = s + strlen(s) - 1;
/*...*/

left and right is the same type as parameter s's, i.e. char *, and char
may be signed or unsigned, it is undefined.
No, it's implementation defined.
The sign flag does not
matter, does it?
It does matter. See the recent exceedingly long thread titled:
What's the deal with the "toupper" family?
for all the gory details.
--
Flash Gordon, living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidelines and intro:
http://clc-wiki.net/wiki/Intro_to_clc
Jul 11 '06 #14
lovecreatesbeauty wrote:

[some lines snipped]
int palindrome(const char *s)
{
int pln = 1;
const char *left, *right;

if (s != NULL){
while (FOO){
BAZ;
if (BAR){
++left;
--right;
continue;
} else {
pln = 0; /*charaters occur mismatched, it is not palindrome*/
break;
}
}
}

return pln;
}
Question for Richard -- how can you prefer this to:

int palindrome(const char *s)
{
const char *left, *right;

if (s == NULL)
return 1;

for (;;)
{
if (!FOO)
return 1;

BAZ;

if (!BAR)
return 0;

++left;
--right;
}
}

Specifically, are you comfortable with most of the function
being at an extra level of indent; and all those continues
and breaks?

Jul 12 '06 #15
Old Wolf said:

<ghastly code snipped>
>
Question for Richard -- how can you prefer this to:
<ghastly code snipped>

I don't. I think they're both ghastly.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Jul 12 '06 #16

Richard Heathfield wrote:
Old Wolf said:

<ghastly code snipped>

Question for Richard -- how can you prefer this to:
<ghastly code snipped>

I don't. I think they're both ghastly.
Hello Old Wolf and Richard, could you write some graceful versions of
palindromic function, or people may think you were / are on
windbaggery, they may be eager to read that code from both you. My
ghastly code quoted below first:

/*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. - 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 {
/*charaters occur mismatched, the string is not a palindrome*/
pln = 0;
break;
}
}
}

return pln;
}

Jul 12 '06 #17
lovecreatesbeauty said:
>
Richard Heathfield wrote:
>Old Wolf said:

<ghastly code snipped>
>
Question for Richard -- how can you prefer this to:
<ghastly code snipped>

I don't. I think they're both ghastly.

Hello Old Wolf and Richard, could you write some graceful versions of
palindromic function,
Yes, I could.
or people may think you were / are on windbaggery,
That's great psychology. Why don't you just say something rude about my
mother?

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Jul 12 '06 #18
On 12 Jul 2006 02:39:49 -0700, "lovecreatesbeauty"
<lo***************@gmail.comwrote:
>Richard Heathfield wrote:
>Old Wolf said:

<ghastly code snipped>
>
Question for Richard -- how can you prefer this to:
<ghastly code snipped>

I don't. I think they're both ghastly.

Hello Old Wolf and Richard, could you write some graceful versions of
palindromic function, or people may think you were / are on
windbaggery, they may be eager to read that code from both you. My
ghastly code quoted below first:
<OT>
Yeah, and please do not forget to write donation to public domain
clause so that ramakrishnadeepak and I can leech copies with good
conscience; Ramakrishna to teacher and I as a raw material for
t3d_find_barray_Rbarray_SUBSTRING_n_LONGEST_PALIND ROME()
(note expressiveness in function name)

Just kidding, at least don't do it for me :) I see people sending all
the time all kinds of sleazy psycho enhanced challenges formulated in
couple simple words that take hours or even days to execute. For
example when I recently posted a data-mining algorithm link to
requester who wanted data-mining algorithms in C (who had gotten
nothing from anyone else), few days later there was "Your Uncle"
asking me to prove that I understand or not understand the algorithms
in a (good) link-page I gave...
<snip>
>I'd love to hear anything you have to say about this topic. I believe it has forensic dimensions. bfx
</snip>

Ehh, dream on 'Uncle'.
Juuso Hukkanen
www.tele3d.com
(to reply by e-mail set addresses month and year to correct)
"t3d programming language" and the structure of t3d function prototype
are trademarks of Juuso Hukkanen. (Currently discussing the
transfer of those to a major charity organization).
</OT>


Jul 12 '06 #19
ra***************@gmail.com wrote:
I 've to submit a program on c.Can any one help me
plz.........The problem is like this::

Write a program which computes the largest palindrome substring of a
string.

Input:
The input is the string whose largest palindrome substring should be
found.

Output:
The output displays the largest palindrome substring.

Sample Input:
Enter the string: Habitat

Sample Output:
The longest palindrome substring of Habitat is: tat
is it september already?

--
Nick Keighley

Jul 12 '06 #20
Nick Keighley wrote:
ra***************@gmail.com wrote:
(fx:snip)
>Sample Output:
The longest palindrome substring of Habitat is: tat

is it september already?
For the last ten years.

--
Chris "ish" Dollin
"Who are you? What do you want?" /Babylon 5/

Jul 12 '06 #21
lovecreatesbeauty posted:

Hello Old Wolf and Richard, could you write some graceful versions of
palindromic function

Here's how I'd probably go about it.

I'd start off with a function signature like:

int IsPalindrome(char const *p,
int (* const Equality)(char,char),
int (* const Immune)(char) )

I'd create a pointer to the start of the string, and a pointer to the end
of the string. I'd increment the former, and decrement the latter, each
time firstly checking if either char is immune from the test (e.g. non-
alphabetic), and then checking their equality.

The bare bones would be something like:
(Unchecked code, likely to contain errors)
#include <stddef.h>
#include <string.h>

int IsPalindrome(char const *p_start,
int (* const Equal)(char,char),
int (* const Immune)(char) )
{
size_t const len = strlen(p);

char const *p_end = p_start + (len - 1);

for( ; ; ++p_start, --p_end)
{
if(p_end - p_start <= 0) return 1;

while(Immune(*p_start)) ++p_start;
while(Immune(*p_end)) --p_end;

if(p_end - p_start <= 0) return 1;

if(!Equal(*p_start,*p_end)) return 0;
}
}
--

Frederick Gotham
Jul 12 '06 #22

In article <1d************@news.flash-gordon.me.uk>, Flash Gordon <sp**@flash-gordon.me.ukwrites:
lovecreatesbeauty wrote:
Flash Gordon wrote:
lovecreatesbeauty wrote:
if (toupper(*left) == *right || tolower(*left) == *right){
I would replace the above with either
if (toupper(*left) == toupper(*right)){
or
if (tolower(*left) == tolower(*right)){
I expect this one may save one additional function call.
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.

If it *is* skipped, on a typical modern general-purpose CPU, you're
likely to have a branch misprediction penalty, so there go any
savings.
Or if toupper and tolower are implemented as efficient macros your
method might be slower.
Indeed it might, since it introduces another branch.

More important, though, is that there's no evidence that this
"optimization" is at all justified. Is the runtime of this loop
crucial to any outside process? Who needs to shave a millisecond
off the time taken to find the palindrome?

So what we have is premature optimization, and probably adverse
optimization at that.

--
Michael Wojcik mi************@microfocus.com

Poe said that poetry was exact.
But pleasures are mechanical
and know beforehand what they want
and know exactly what they want. -- Elizabeth Bishop
Jul 12 '06 #23
Michael Wojcik wrote:
In article <1d************@news.flash-gordon.me.uk>, Flash Gordon <sp**@flash-gordon.me.ukwrites:
lovecreatesbeauty wrote:
Flash Gordon wrote:
>lovecreatesbeauty wrote:
>> if (toupper(*left) == *right || tolower(*left) == *right){
>I would replace the above with either
> if (toupper(*left) == toupper(*right)){
>or
> if (tolower(*left) == tolower(*right)){
>
I expect this one may save one additional function call.

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.
Yeah! It's more efficient after exchange those two function call, make
it as:

if (tolower(*left) == *right || toupper(*left) == *right){

/* the previous one was:
if (toupper(*left) == *right || tolower(*left) == *right){ */

Palindromes are rather common English (so the latest change may not be
more efficient), and no one knows what the largest palindrome will be.

Madam, I'm Adam!
A man, a plan, a canal - Panama!
Able was I, ere I saw Elba.
Ana voli Milovana

lovecreatesbeauty

Jul 12 '06 #24

lovecreatesbeauty wrote:
>
/*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;
}
The code is mainly derived from Robert Gamble's code. Thanks.

Jul 13 '06 #25

In article <11**********************@p79g2000cwp.googlegroups .com>, "lovecreatesbeauty" <lo***************@gmail.comwrites:
Michael Wojcik wrote:
lovecreatesbeauty wrote:
> if (toupper(*left) == *right || tolower(*left) == *right){

I expect this one may save one additional function call.
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.

Yeah! It's more efficient after exchange those two function call, make
it as:

if (tolower(*left) == *right || toupper(*left) == *right){
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.

--
Michael Wojcik mi************@microfocus.com

Do not "test" parts, as this may compromise sensitive joinery. Those who
suffer difficulty should abandon the enterprise immediately. -- Chris Ware
Jul 14 '06 #26
Michael Wojcik wrote:
In article <11**********************@p79g2000cwp.googlegroups .com>, "lovecreatesbeauty" <lo***************@gmail.comwrites:
>Michael Wojcik wrote:
>>>>>lovecreatesbeauty wrote:
>> if (toupper(*left) == *right || tolower(*left) == *right){
I expect this one may save one additional function call.
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.
Yeah! It's more efficient after exchange those two function call, make
it as:

if (tolower(*left) == *right || toupper(*left) == *right){

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,
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).
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.
Even where performance does matter I would still concentrate on clarity,
correctness and maintainability first. If, having done that, I can't get
the required performance, I will look at what the compiler produces and
see if that suggests any optimisations (with one Pascal compiler,
replacing constant calculations with the *commented* calculated value
was a good optimisation since it did the calculation at run time!). When
that does not allow me to optimise it enough, I have stopped wasting
time trying to guess how to get the compiler to produce good enough code
and written the *small* (usually) critical section directly in
assembler, but I've only needed to do that with one small piece of code
I knew would be hard for any compiler to optimise on the DSP processor,
and one entire project where the processor was a real bitch for a
horrible processor and the hardware was a long way under specified.

Oh, and ensuring that where lots of large structures are passed around
they are passed by reference (this was Pascal, so passing a pointer to
the struct in C) rather than passing the large structures by value.
Having done that, I politely pointed out to my manager that the original
design was completely wrong, so even though I have more than doubled the
speed of the code (thus making it work) it should not have been anything
like that design in the first place. Then he told me that he had
originally designed it, which made me very glad I had been polite about
it! Another thing this taught me was the importance of designing
software so that when overloaded it degrades gracefully rather than
having the first overload cause a slow down leading to more overload
leading to more slow down leading very quickly to it all grinding to a
halt as soon as the critical threshold is exceeded. Admittedly this last
lesson is not appropriate in all instances, but it does often mean that
people will get possibly a little annoyed about some jerkiness instead
of extremely pissed off with it suddenly not working.
--
Flash Gordon, living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidelines and intro:
http://clc-wiki.net/wiki/Intro_to_clc
Jul 14 '06 #27
av
On 14 Jul 2006 23:01:02 +0200, Flash Gordon wrote:
>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.
Jul 15 '06 #28
av said:
On 14 Jul 2006 23:01:02 +0200, Flash Gordon wrote:
>>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.
Hold that thought. It will serve you well.

Until you realise that optimisation is a function of design rather than
programming, you're not ready. By the time that lesson sinks in, you're
probably well on the way to becoming an expert anyway.
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Jul 15 '06 #29
av <av@ala.awrote:
On 14 Jul 2006 23:01:02 +0200, Flash Gordon wrote:
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.
First rule of optimisation: Don't Do It.
Second rule of optimisation (for expert only): Don't Do It Yet.

HTH; HAND.

Richard
Jul 17 '06 #30
av
On Mon, 17 Jul 2006 06:36:58 GMT, rl*@hoekstra-uitgeverij.nl (Richard
Bos) wrote:
>av <av@ala.awrote:
>On 14 Jul 2006 23:01:02 +0200, Flash Gordon wrote:
>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.

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
>HTH; HAND.

Richard
Jul 17 '06 #31
av posted:

noone become expert if not try to optimize
so if noone of not expert optimize there will be no expert

noone learn read and rite witout lerning alfabet & lerning gramar & how 2
spel.

--

Frederick Gotham
Jul 17 '06 #32
av <av@ala.awrites:
On Mon, 17 Jul 2006 06:36:58 GMT, rl*@hoekstra-uitgeverij.nl (Richard
Bos) wrote:
>>av <av@ala.awrote:
>>On 14 Jul 2006 23:01:02 +0200, Flash Gordon wrote:
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.

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
Fortunately, real life doesn't work that way. There are plenty of
experts who are capable of optimizing code. If they're experts, they
know when not to do it.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jul 17 '06 #33

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Lorin Leone | last post by:
Can anyone help me modify the program so that it recognizes strings like "Anna" as palindromes. To make the program "case-insensitive." using the built-in C++ function "toupper". and so that it...
23
by: Amar Prakash Tripaithi | last post by:
Dan Hoey, who had recently graduated, wrote a C program to look for and construct the following beauty: A man, a plan, a caret, a ban, a myriad, a sum, a lac, a liar, a hoop, a pint, a catalpa,...
4
by: outofmymind | last post by:
hi, im trying to solve the following question: Create a class responsible for determining whether a string is a palindrome. Show your test cases. Palindome mypal("bob"); ...
3
by: colinNeedsJavaHelp | last post by:
I am still having an exceptional amount of trouble with java. This is my new assignment, if anyone can help I would greatly appreciate it. I don't even know where to start. A word or phrase in...
2
by: Synapse | last post by:
aloha people! I need help in my java palindrome program. It's a 5-digit palindrome checker. The code below is running good but i got a few problems on it. If I enter 33633, 11211, 45554, it will...
20
by: Wabz | last post by:
Hello mates, Does anyone know how to write a function that tests if an integer is a palindrome in C language?
4
by: vicestner | last post by:
Write a Java program that prints the longest palindrome in an input file to standard output. A palindrome is a string whose reverse is the same as the original. Ignore case, whitespace, and...
5
by: rubyhuang | last post by:
i'm a new perl learner, this is the first perl task i will do. please help me. The user can input a string, and then a script will check to see if the string is a palindrome or not, displaying the...
2
by: bigtd08 | last post by:
help writing this palindrome program for my c++ class. HERE WHAT THE CODE SHOULD BE LIKE. Write a program that takes a line of input from the keyboard and check to see if that line is a palindrome....
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.