EFFICIENCY question: need help from the C geniuses

M

mark

Try this:

int addbitwise (int x[], int y[]) {
int xx, yy

xx = x[4] + ((x[3] + ((x[2] + ((x[1] + (x[0] << 1)) << 1)) << 1)) << 1);
yy = y[4] + ((y[3] + ((y[2] + ((y[1] + (y[0] << 1)) << 1)) << 1)) << 1);
return xx + yy >= 32;
}

i will... btw, i guess i wasn't so clear before but i am optimizing
for speed and all other factors (code size, portability, etc.) in this
case are not a concern. thanks...

mark.
 
P

Paul Hsieh

Christian Bau said:
He used a unix tool to measure the total execution time of the program,
which was 0.041 seconds. The function in question was called once. I
would guess that the execution time of that function is less than a
microsecond, so the ratio overhead / execution time is about 41,000 to
1. If he replaced his function with yours which as a rough guess would
be ten times faster, then his measurement will still be 0.041 seconds.

Yes, I noticed that part of the thread. However, the fact that he
made an error if understanding order of magnitude of various things
going on in program execution, it doesn't preclude the possibility
that he correctly profiled at the beginning, but just followed it up
by doing a detailed analysis of a small section of code but measured
it wrong.

But this is comp.lang.c, not comp.programming.realworld so discussions
of what really matters to programmers is probably OT around here.
 
R

Richard Heathfield

Paul said:
But this is comp.lang.c, not comp.programming.realworld so discussions
of what really matters to programmers is probably OT around here.

Speak for yourself. Portable C programming is topical here, and portable C
programming matters for those who use this newsgroup regularly. If it
didn't, they wouldn't use it, would they?

Yes, there are things that matter to me other than portable C programming,
but there are *other newsgroups* for discussing such things.
 
G

Grumble

Paul said:
In this case, I think I've won on all those except possibly
portability to systems for which integers are less than 6 bits.

But I thought:

(CHAR_BITS >= 8) && (sizeof int >= sizeof char)

Am I mistaken?
 
C

CBFalconer

Grumble said:
But I thought:

(CHAR_BITS >= 8) && (sizeof int >= sizeof char)

Am I mistaken?

Please show me how you code integers, which must range from -32767
through 32767, in those 6 bits. These are min/max values allowed
for INT_MIN and INT_MAX.

When you show me, I believe I can immediately create something
that compresses all zip, bzip2, gzip, etc. files by a further
factor of better than two.
 
G

Grumble

CBFalconer said:
Please show me how you code integers, which must range from -32767
through 32767, in those 6 bits. These are min/max values allowed
for INT_MIN and INT_MAX.

When you show me, I believe I can immediately create something
that compresses all zip, bzip2, gzip, etc. files by a further
factor of better than two.

Chuck,

Are you responding to me?

In any case, are you saying that an int must be at least 16 bits wide?
 
A

Arthur J. O'Dwyer

Chuck,

Are you responding to me?

In any case, are you saying that an int must be at least 16 bits wide?

I don't know to whom he's responding, but that is correct -- ints
must be at least 16 bits wide in order to be able to represent values
from INT_MIN to INT_MAX. And wider if INT_MIN or INT_MAX have larger
absolute values.

-Arthur
 
C

CBFalconer

Grumble said:
Are you responding to me?

In any case, are you saying that an int must be at least 16 bits wide?

I am saying that they have to represent that range of values.
They must also be constructed out of binary weighted bits. So, if
you can show me how to meet standards with a six bit integer, I
can do other miraculous things :)
 
P

Paul Hsieh

Grumble said:
But I thought:

(CHAR_BITS >= 8) && (sizeof int >= sizeof char)

Am I mistaken?

Ok, then maybe it is portable. What do I look like? A language lawyer?
 
B

Ben Pfaff

Grumble said:
But I thought:

(CHAR_BITS >= 8) && (sizeof int >= sizeof char)

Am I mistaken?

No, although there is no S in CHAR_BIT and you forgot the
mandatory parentheses in this usage of the sizeof operator. A
byte in C is at least 8 bits, and every object is at least one
byte in size.
 
G

Grumble

Ben said:
No, although there is no S in CHAR_BIT and you forgot the
mandatory parentheses in this usage of the sizeof operator. A
byte in C is at least 8 bits, and every object is at least one
byte in size.

CHAR_BIT. No 'S'. Got that.

Are parentheses mandatory when sizeof is used to evaluate a type
instead of an object?
 

Ask a Question

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.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,152
Latest member
LorettaGur
Top