473,789 Members | 2,898 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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&0x7 f);
}

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 1524

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********@gma il.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&0x7 f);
}

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********@gma il.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********@gma il.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_Keit h) 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.stanfor d.edu> wrote:
"nelu" <ta********@gma il.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&0x7 f);
} 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********@gma il.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************ **********@z14g 2000cwz.googleg roups.com>,
nelu <ta********@gma il.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

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

Similar topics

10
1716
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 processes. It's been running fine for years, but now that I've moved to faster hardware, I'm running into a problem that surfaces more frequently as the hardware I'm using gets better/faster -- it seems like some sort of race condition issue. ...
22
1921
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
12619
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 method was a piece of C code which turned out to be incorrect and incomplete but by modifieing it would still be usuable. The first method was this piece of text:
16
1850
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. def p(self, paragraph):
25
1722
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
5669
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
1975
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
2532
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) return s def descrambleLine(line):
84
3975
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. 2. Client side processing: Web server gets form input and passes it to PHP which includes the Javascript written in a way to make the form input processed on the client side and rendered (probably using DOM function calls) on that side as...
0
9666
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
10139
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9984
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7529
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6769
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5418
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5551
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4093
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2909
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.