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 11 6618
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/
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
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>>
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
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 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.
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; }
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!
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!
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: beliavsky |
last post by:
By mistake I coded something like
print ("1" > 1)
and got the result "True". Comparing an integer and a string seems
meaningless to me, and I would prefer to have an exception thrown. Can...
|
by: Jeff Melvaine |
last post by:
I note that I can write expressions like "1 << 100" and the result is stored
as a long integer, which means it is stored as an integer of arbitrary
length. I may need to use a large number of...
|
by: Elijah Bailey |
last post by:
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...
|
by: Nicholas |
last post by:
How can I compare char* with integers and characters contained in the str,
where integers can be one digit or more?
void Access(char *str) {
char *pt = str;
while (pt != '0') {
if...
|
by: John |
last post by:
Hi,
I encountered a strange problem while debugging C code for a
Windows-based application in LabWindows CVI V5.5, which led me to
write the test code below. I tried this code with a different...
|
by: purifier |
last post by:
The problem is to write a program in 'C' to find the greatest of 2 given
numbers... Easy? huh
here's the catch
do not use 'if' or any conditional statements
if u want it to be a little more...
|
by: Donald Grove |
last post by:
If I have two arrays, what is a good paradigm for comparing what is in
them, to determine what elements they share, or don't share?
Specifically, each array could potentially contain the integers...
|
by: Pugi! |
last post by:
hi,
I am using this code for checking wether a value (form input) is an
integer and wether it is smaller than a given maximum and greater then
a given minimum value:
function...
|
by: arnuld |
last post by:
It is quite an ugly hack but it is all I am able to come up with for
now :-( and it does the requires work. I want to improve the program, I
know you people have much better ideas ;-)
/* C++...
|
by: RN1 |
last post by:
Sometimes I find that though I am comparing 2 integers, the result
turns out to be unexpected.
For e.g. an ASP page encapsulates recordset paging.
<%
Dim iPage,iPageCounter
...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
| |