comparing long integers

E

Elijah Bailey

I have two char arrays of size k.
I want to know which one is bigger (exactly like for instance
I compare two ints/longs/etc.).

What is the fastest way to do this? k <= 10 usually for my application.
I tried bcmp / a loop for comparison, but it seems they are very slow
compared to comparing longs...Any ideas?

I tried splitting the array into longs and comparing, but then
I face the high endian/low endian problem on some machines.
My array has data such that the 1st byte is the least significant
byte (and the kth byte is the most significant byte)

Thanks,
--Elijah
 
J

Jonathan Turkanis

Elijah Bailey said:
I have two char arrays of size k.
I want to know which one is bigger (exactly like for instance
I compare two ints/longs/etc.).

What is the fastest way to do this? k <= 10 usually for my application.
I tried bcmp / a loop for comparison, but it seems they are very slow
compared to comparing longs...Any ideas?

I tried splitting the array into longs and comparing, but then
I face the high endian/low endian problem on some machines.
My array has data such that the 1st byte is the least significant
byte (and the kth byte is the most significant byte)

Thanks,
--Elijah

What comparison criterion do you want to use?
std::char_traits<char>::compare should provide a fast lexicographical
comparison.

Jonathan
 
E

Eric Sosman

Elijah said:
I have two char arrays of size k.
I want to know which one is bigger (exactly like for instance
I compare two ints/longs/etc.).

What is the fastest way to do this? k <= 10 usually for my application.
I tried bcmp / a loop for comparison, but it seems they are very slow
compared to comparing longs...Any ideas?

I tried splitting the array into longs and comparing, but then
I face the high endian/low endian problem on some machines.
My array has data such that the 1st byte is the least significant
byte (and the kth byte is the most significant byte)

If the arrays were in most-to-least significant
order you could use the memcmp() function. This is
likely to be as least as fast as anything you could
write in plain C, if not faster (although, of course,
the Standard makes no such guarantee). So if you are
able to redesign things so the arrays appear 'tother
way around, it might be worth while to do so.

Failing that, you could roll your own:

int rev_memcmp(const void *pp, const void *qq, size_t n)
{
const unsigned char *p = pp, *q = qq;
while (n-- > 0) {
if (p[n] != q[n])
return (p[n] < q[n]) ? -1 : +1;
}
return 0;
}
 
D

Donovan Rebbechi

I tried splitting the array into longs and comparing, but then
I face the high endian/low endian problem on some machines.

If you're worried about that, you would need to write "conditional code"
that depends on endianness (for example, using #ifdef or templates if you
like) and make endianness a compile-time configurable option (depending on
your tools, you could automate detection/configuration of this)

Anyway, that's what I do whenever I write code that is endian-dependent
(usually applies to code that reads binary data from disk)

Cheers,
 
B

Barry Schwarz

I have two char arrays of size k.
I want to know which one is bigger (exactly like for instance
I compare two ints/longs/etc.).

What don't you like about the standard memcmp and strncmp functions.
What is the fastest way to do this? k <= 10 usually for my application.
I tried bcmp / a loop for comparison, but it seems they are very slow
compared to comparing longs...Any ideas?

I tried splitting the array into longs and comparing, but then
I face the high endian/low endian problem on some machines.
My array has data such that the 1st byte is the least significant
byte (and the kth byte is the most significant byte)

Thanks,
--Elijah



<<Remove the del for email>>
 
P

Peter Nilsson

Eric Sosman said:
Elijah said:
I have two char arrays of size k.
I want to know which one is bigger (exactly like for instance
I compare two ints/longs/etc.).

What is the fastest way to do this? k <= 10 usually for my application.
I tried bcmp / a loop for comparison, but it seems they are very slow
compared to comparing longs...Any ideas?

I tried splitting the array into longs and comparing, but then
I face the high endian/low endian problem on some machines.
My array has data such that the 1st byte is the least significant
byte (and the kth byte is the most significant byte)

If the arrays were in most-to-least significant
order you could use the memcmp() function. This is
likely to be as least as fast as anything you could
write in plain C, if not faster (although, of course,
the Standard makes no such guarantee). So if you are
able to redesign things so the arrays appear 'tother
way around, it might be worth while to do so.

Failing that, you could roll your own:

int rev_memcmp(const void *pp, const void *qq, size_t n)
{
const unsigned char *p = pp, *q = qq;
while (n-- > 0) {
if (p[n] != q[n])
return (p[n] < q[n]) ? -1 : +1;
}
return 0;
}

Neither memcmp nor Eric's rev_memcmp will compare chars as integers,
rather they compare raw bytes, so negative (plain) char values would
typically compare 'higher' than positive values.
 
N

Nick Landsberg

If I understand the problem statement,
the original poster wants to compare the sums(totals)
of arrayA vs. arrayB. I see no way but to sum both
arrays and compare the values.

There is no language construct that I know of to do it
for him.

Silverlock



Peter said:
Eric Sosman said:
Elijah said:
I have two char arrays of size k.
I want to know which one is bigger (exactly like for instance
I compare two ints/longs/etc.).

What is the fastest way to do this? k <= 10 usually for my application.
I tried bcmp / a loop for comparison, but it seems they are very slow
compared to comparing longs...Any ideas?

I tried splitting the array into longs and comparing, but then
I face the high endian/low endian problem on some machines.
My array has data such that the 1st byte is the least significant
byte (and the kth byte is the most significant byte)

If the arrays were in most-to-least significant
order you could use the memcmp() function. This is
likely to be as least as fast as anything you could
write in plain C, if not faster (although, of course,
the Standard makes no such guarantee). So if you are
able to redesign things so the arrays appear 'tother
way around, it might be worth while to do so.

Failing that, you could roll your own:

int rev_memcmp(const void *pp, const void *qq, size_t n)
{
const unsigned char *p = pp, *q = qq;
while (n-- > 0) {
if (p[n] != q[n])
return (p[n] < q[n]) ? -1 : +1;
}
return 0;
}


Neither memcmp nor Eric's rev_memcmp will compare chars as integers,
rather they compare raw bytes, so negative (plain) char values would
typically compare 'higher' than positive values.
 
J

J. J. Farrell

I have two char arrays of size k.
I want to know which one is bigger (exactly like for instance
I compare two ints/longs/etc.).

What is the fastest way to do this?

Please don't cross-post questions like this to groups
that discuss different languages - it's likely to end
up with a confusing thread. If you need the question
answered for both languages, this is one of the
occasions where posting the same question to each
group in separate messages would be appropriate.
 
E

Elijah Bailey

If you time this code compared for n = 8 or 16 and
instead use 2 or 4 longs, and then use the longs for
comparison instead of char *, you would see that
the speed of the long comparison is at least twice
as fast. Is there a way to speed this loop further
if k = 8?

Eric Sosman said:
Elijah said:
I have two char arrays of size k.
I want to know which one is bigger (exactly like for instance
I compare two ints/longs/etc.).

What is the fastest way to do this? k <= 10 usually for my application.
I tried bcmp / a loop for comparison, but it seems they are very slow
compared to comparing longs...Any ideas?

I tried splitting the array into longs and comparing, but then
I face the high endian/low endian problem on some machines.
My array has data such that the 1st byte is the least significant
byte (and the kth byte is the most significant byte)

If the arrays were in most-to-least significant
order you could use the memcmp() function. This is
likely to be as least as fast as anything you could
write in plain C, if not faster (although, of course,
the Standard makes no such guarantee). So if you are
able to redesign things so the arrays appear 'tother
way around, it might be worth while to do so.

Failing that, you could roll your own:

int rev_memcmp(const void *pp, const void *qq, size_t n)
{
const unsigned char *p = pp, *q = qq;
while (n-- > 0) {
if (p[n] != q[n])
return (p[n] < q[n]) ? -1 : +1;
}
return 0;
}
 
C

CBFalconer

Nick said:
If I understand the problem statement,
the original poster wants to compare the sums(totals)
of arrayA vs. arrayB. I see no way but to sum both
arrays and compare the values.

There is no language construct that I know of to do it
for him.

Kindly do not toppost. Doing so has lost all sensible quotation.

As I understood it he wanted to compare strings holding numerical
quantities, i.e. the output of (typically) sprintf("%d", n); But
it was far from clear.
 
C

CBFalconer

Elijah said:
If you time this code compared for n = 8 or 16 and
instead use 2 or 4 longs, and then use the longs for
comparison instead of char *, you would see that
the speed of the long comparison is at least twice
as fast. Is there a way to speed this loop further
if k = 8?

Kindly do not toppost. Doing so has lost all context, and makes
your query useless.
 
D

Dan Pop

In said:
What don't you like about the standard memcmp and strncmp functions.

Well, strncmp is useless for this purpose. If both arrays are identical
up to (and including) the first null byte, it would declare them equal...

Dan
 
D

Dan Pop

In said:
Kindly do not toppost. Doing so has lost all sensible quotation.

As I understood it he wanted to compare strings holding numerical
quantities, i.e. the output of (typically) sprintf("%d", n); But
it was far from clear.

As usual, you forgot to engage your brain. It was crystal clear:

My array has data such that the 1st byte is the least significant
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
byte (and the kth byte is the most significant byte)
^^^^

Dan
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top