473,324 Members | 2,313 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

which code runs faster?

I know I can use unsinged byte, but I need it for java, which code runs
faster in C?

int f1(char b) {
return (b&0x80)|(b&0x7f);
}

or

int f2(char b) {
return b>0?b:256+bl
}

?

In my tests f2 performs better. I need a better opinion. :-)
Thanks.

Dec 12 '05 #1
16 1491

nelu wrote:

In my tests f2 performs better. I need a better opinion. :-)


Ooops, my java tests... f1 performs better in C, it seems. (gcc -O3)

Dec 12 '05 #2
"nelu" <ta********@gmail.com> writes:
I know I can use unsinged byte, but I need it for java, which code runs
faster in C?

int f1(char b) {
return (b&0x80)|(b&0x7f);
}

or

int f2(char b) {
return b>0?b:256+bl
}


return b & 0xff;
--
"...what folly I commit, I dedicate to you."
--William Shakespeare, _Troilus and Cressida_
Dec 12 '05 #3

Ben Pfaff wrote:
return b & 0xff;


Right, still, which of the ones I wrote should be faster?

Dec 12 '05 #4
"nelu" <ta********@gmail.com> writes:
Ben Pfaff wrote:
return b & 0xff;


Right, still, which of the ones I wrote should be faster?


There's no way to say. It will vary from one machine to another
and one compiler to another. If you want an answer for your
implementation, try them both.
--
"C has its problems, but a language designed from scratch would have some too,
and we know C's problems."
--Bjarne Stroustrup
Dec 12 '05 #5
"nelu" <ta********@gmail.com> writes:
Ben Pfaff wrote:
return b & 0xff;


Right, still, which of the ones I wrote should be faster?


The only way to know is to measure it. The answer is likely to vary
depending on the compiler, the system you're using, and any
optimization options used.

Unless the code is critical (meaning that you've measured it and found
that its performance has a *significant* impact on the performance of
your program), you should probably just write the clearest code
possible and let the compiler worry about 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.
Dec 12 '05 #6
In article <87************@benpfaff.org>,
Ben Pfaff <bl*@cs.stanford.edu> wrote:
"nelu" <ta********@gmail.com> writes:
I know I can use unsinged byte, but I need it for java, which code runs
faster in C? int f1(char b) {
return (b&0x80)|(b&0x7f);
} int f2(char b) {
return b>0?b:256+bl
}

return b & 0xff;


Or, if it is known that CHAR_BIT is 8, then

return (int)(unsigned char)b;

This has the advantage that on compilers where char is already unsigned
and 2s complement, this will become just return b .

If the environment might not be 2s complement, then f1() and f2()
are not equivilent. On a signed-magnitude machine, (b&0x80) might
be equivilent to b&0x00. On SM, -1 might be 1|0000001
and f2() would produce 255 but f1() would probably produce 0x01.
b & 0xff has the same difficulty. (int)(unsigned char)b would
produce the same result as f2() on such a machine.

Thus, the designer must decide whether the intent is to get at the
native bit pattern, or to get at the value "as if" it were 2s complement.
--
Programming is what happens while you're busy making other plans.
Dec 12 '05 #7
On 12 Dec 2005 13:35:09 -0800, in comp.lang.c , "nelu"
<ta********@gmail.com> wrote:
I know I can use unsinged byte, but I need it for java, which code runs
faster in C?


neither.
both.
There's no way to say, its implementation specific and might even
depend on what else the computer was doing, etc etc. Why does it
matter? Do you know that you have a performance bottleneck here?

There are three rules about optimisation.
1) don't do it
2) don't do it YET
3) you can guess what this one is.
----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Dec 12 '05 #8
Walter Roberson wrote:
Or, if it is known that CHAR_BIT is 8, then

return (int)(unsigned char)b;

This has the advantage that on compilers where char is already unsigned
and 2s complement, this will become just return b .

If the environment might not be 2s complement, then f1() and f2()
are not equivilent. On a signed-magnitude machine, (b&0x80) might
be equivilent to b&0x00. On SM, -1 might be 1|0000001
and f2() would produce 255 but f1() would probably produce 0x01.
b & 0xff has the same difficulty. (int)(unsigned char)b would
produce the same result as f2() on such a machine.

Thus, the designer must decide whether the intent is to get at the
native bit pattern, or to get at the value "as if" it were 2s complement.
--
Programming is what happens while you're busy making other plans.


I was wondering which is the best way to get the value of a byte
between 0 and 255. The difference between the two functions in C is big
compared to the same functions written in JAVA. The bit operations are
a lot faster than the if function on my machine (AMD64) when compiled
with -O3. I thought there would've been an easy answer since both codes
look really simple.
I guess the safest way is to use the if function for what I need since
bit operations can have different results in that form from machine to
machine, right?

Dec 12 '05 #9
In article <11**********************@z14g2000cwz.googlegroups .com>,
nelu <ta********@gmail.com> wrote:
Walter Roberson wrote:
Or, if it is known that CHAR_BIT is 8, then return (int)(unsigned char)b; Thus, the designer must decide whether the intent is to get at the
native bit pattern, or to get at the value "as if" it were 2s complement.
I was wondering which is the best way to get the value of a byte
between 0 and 255. I guess the safest way is to use the if function for what I need since
bit operations can have different results in that form from machine to
machine, right?


Suppose, though, that CHAR_BIT is not 8. Imagine, for example, that
the "Windows" key operated by setting the 9th bit. How is your function
defined in such a case?

"byte" is a general concept, but the size of a "byte" varies
with different architectures. Same with "char". "byte" and "char"
are not exactly synonyms either.
If I understand correctly something I have read in passing, in Java
the size of characters is fixed, defined as being the same on
every implementation. This might perhaps affect your choice of
semantics you define.
--
Prototypes are supertypes of their clones. -- maplesoft
Dec 13 '05 #10
ro******@ibd.nrc-cnrc.gc.ca (Walter Roberson) writes:
In article <11**********************@z14g2000cwz.googlegroups .com>,
nelu <ta********@gmail.com> wrote:
Walter Roberson wrote:
Or, if it is known that CHAR_BIT is 8, then return (int)(unsigned char)b; Thus, the designer must decide whether the intent is to get at the
native bit pattern, or to get at the value "as if" it were 2s complement.

I was wondering which is the best way to get the value of a byte
between 0 and 255.

I guess the safest way is to use the if function for what I need since
bit operations can have different results in that form from machine to
machine, right?


Suppose, though, that CHAR_BIT is not 8. Imagine, for example, that
the "Windows" key operated by setting the 9th bit. How is your function
defined in such a case?

"byte" is a general concept, but the size of a "byte" varies
with different architectures. Same with "char". "byte" and "char"
are not exactly synonyms either.


No, they're not synonyms, but a "byte" is by definition the size of
type "char" in C. (Of course the terms are used differently outside
the context of C.)

--
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.
Dec 13 '05 #11
nelu wrote:
Keith Thompson wrote:
No, they're not synonyms, but a "byte" is by definition the size of
type "char" in C. (Of course the terms are used differently outside
the context of C.)
In Java byte (8 bits) is the equivalent of signed char.


No. In C, signed char needn't be two's complement. You should be trying
to
use int8_t, rather than signed char.
char in java has 2 bytes.

I have never used a machine that has more or less than 8 bits in 1 byte
(not even on Sinclair Spectrum).
Some of the older PDP implementations had non-8-bit byte sizes.
Can you tell me how they work?
Like any other implementation, just with wider than usual bytes.
Is it possible for them to comunicate with 8 bits bytes machines and if yes,
how?


The same way that 8-bit byte machines communicate through 7-pin din
connectors.
Any number of ways. But none of them are necessarily topical in
comp.lang.c.

--
Peter

Dec 13 '05 #12

Peter Nilsson wrote:
No. In C, signed char needn't be two's complement. You should be trying
to
use int8_t, rather than signed char.


True, I was thinking about char the way I used it on my machine :-).

Dec 13 '05 #13

Keith Thompson wrote:

No, they're not synonyms, but a "byte" is by definition the size of
type "char" in C. (Of course the terms are used differently outside
the context of C.)


In Java byte (8 bits) is the equivalent of signed char. char in java
has 2 bytes.
I have never used a machine that has more or less than 8 bits in 1 byte
(not even on Sinclair Spectrum). Can you tell me how they work? Is it
possible for them to comunicate with 8 bits bytes machines and if yes,
how?

Thanks

Dec 13 '05 #14
On 12 Dec 2005 15:09:08 -0800, in comp.lang.c , "nelu"
<ta********@gmail.com> wrote:
I was wondering which is the best way to get the value of a byte
between 0 and 255.


In C, a byte is the same size as a char. You get its value by
examining it. No casts are needed.

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Dec 13 '05 #15
Mark McIntyre <ma**********@spamcop.net> writes:
On 12 Dec 2005 15:09:08 -0800, in comp.lang.c , "nelu"
<ta********@gmail.com> wrote:
I was wondering which is the best way to get the value of a byte
between 0 and 255.


In C, a byte is the same size as a char. You get its value by
examining it. No casts are needed.


Unless it's a signed char (or a plain char and plain char happens to
be signed); then you can cast it to unsigned char.

--
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.
Dec 14 '05 #16
On 12 Dec 2005 20:22:02 -0800, "Peter Nilsson" <ai***@acay.com.au>
wrote:
nelu wrote:

<snip>
I have never used a machine that has more or less than 8 bits in 1 byte
(not even on Sinclair Spectrum).


Some of the older PDP implementations had non-8-bit byte sizes.

Terminology: this sounds like you mean different C implementations on
one "PDP" machine, or several "PDP" machines that implement a common
architecture. In fact Programmed Data Processor was the name used for
a series of machines by DEC (Digital Equipment Corporation) that were
mostly quite different, some with multiple implementations. There were
four models of PDP-10 namely KA-10, KI-10, KL-10, KS-10 which
implemented the same architecture, which was nearly the same as PDP-6.
There were several models of PDP-8 of which I recall 8/s, 8/i, 8/e,
8/m and 8/a, which implemented one architecture, nearly the same as
PDP-5 and similar to PDP-12 aka LINC-12, but very different from -6
and -10. PDP-7 and -9 were similar, and I believe also similar to -4
and -1, but different from -6/10 and -5/8/12. There were many
(numbered) models of PDP-11 like 11/20, 11/40, 11/45, 11/34, 11/70
etc. all implementing basically the same architecture, different from
all other PDP series machines.

There were also quite a few non-DEC machines with non-8-bit bytes.
Can you tell me how they work?


Like any other implementation, just with wider than usual bytes.
Is it possible for them to comunicate with 8 bits bytes machines and if yes,
how?


The same way that 8-bit byte machines communicate through 7-pin din
connectors.
Any number of ways. But none of them are necessarily topical in
comp.lang.c.


In the early days of the Internet and before that ARPANET, this was a
significant issue and there are a number of features (still) specified
especially in FTP to deal with data exchange with systems having a
byte other than 8 bits, no longer used today except perhaps
occasionally by mistake.

- David.Thompson1 at worldnet.att.net
Dec 19 '05 #17

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

Similar topics

10
by: Vineet | last post by:
If anybody has any insight into this problem I'm running into I would really appreciate if you could write to me... I'm running a simple C++ program on Solaris 8 that forks and execs a bunch of...
22
by: The Doctor | last post by:
Hi all, Recently I was asked the question about whether ++U or U++ is faster if U is a user defined type. What's the answer? Thank you
65
by: Skybuck Flying | last post by:
Hi, I needed a method to determine if a point was on a line segment in 2D. So I googled for some help and so far I have evaluated two methods. The first method was only a formula, the second...
16
by: John Salerno | last post by:
My initial feeling is that concatenation might take longer than substitution, but that it is also easier to read: def p(self, paragraph): self.source += '<p>' + paragraph + '</p>\n\n' vs. ...
25
by: Ganesh | last post by:
Hi, This is a question that pertains to pointers in general (C or C++). Which of the following is faster and why? for (int i = 0; i < N; i++) = ... a... (or)
16
by: Brian Tkatch | last post by:
Is there a way to check the order in which SET INTEGRITY needs to be applied? This would be for a script with a dynamic list of TABLEs. B.
17
by: onkar | last post by:
which one runs faster ?? for(i=0;i<100;i++) for(j=0;j<10;j++) a=0; OR for(j=0;j<10;j++) for(i=0;i<100;i++)
23
by: Python Maniac | last post by:
I am new to Python however I would like some feedback from those who know more about Python than I do at this time. def scrambleLine(line): s = '' for c in line: s += chr(ord(c) | 0x80)...
84
by: Patient Guy | last post by:
Which is the better approach in working with Javascript? 1. Server side processing: Web server gets form input, runs it into the Javascript module, and PHP collects the output for document prep....
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.