By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,190 Members | 1,555 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,190 IT Pros & Developers. It's quick & easy.

comparing long integers

P: n/a
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
Nov 14 '05 #1
Share this Question
Share on Google+
11 Replies


P: n/a
In article <e0**************************@posting.google.com >, Elijah Bailey wrote:
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,
--
Donovan Rebbechi
http://pegasus.rutgers.edu/~elflord/
Nov 14 '05 #2

P: n/a
Elijah Bailey wrote:

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;
}

--
Er*********@sun.com
Nov 14 '05 #3

P: n/a
On 22 Jan 2004 13:39:52 -0800, ge******@hotmail.com (Elijah Bailey)
wrote:
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>>
Nov 14 '05 #4

P: n/a
Eric Sosman <Er*********@sun.com> wrote in message news:<40***************@sun.com>...
Elijah Bailey wrote:

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.

--
Peter
Nov 14 '05 #5

P: n/a

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 Nilsson wrote:
Eric Sosman <Er*********@sun.com> wrote in message news:<40***************@sun.com>...
Elijah Bailey wrote:
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.


--
"It is impossible to make anything foolproof because fools are so
ingenious" - A. Bloch

Nov 14 '05 #6

P: n/a
ge******@hotmail.com (Elijah Bailey) wrote in message news:<e0**************************@posting.google. com>...
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.
Nov 14 '05 #7

P: n/a
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 <Er*********@sun.com> wrote in message news:<40***************@sun.com>...
Elijah Bailey wrote:

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;
}

Nov 14 '05 #8

P: n/a
Nick Landsberg wrote:

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.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #9

P: n/a
Elijah Bailey wrote:

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.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #10

P: n/a
In <bu**********@216.39.135.13> Barry Schwarz <sc******@deloz.net> writes:
On 22 Jan 2004 13:39:52 -0800, ge******@hotmail.com (Elijah Bailey)
wrote:
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.


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
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #11

P: n/a
In <40***************@yahoo.com> CBFalconer <cb********@yahoo.com> writes:
Nick Landsberg wrote:

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.


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
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.