Is byte "better" than integer?

This is where you talk about the NXJ software itself, installation issues, and programming talk.

Moderators: 99jonathan, roger, imaqine

Is byte "better" than integer?

Postby titiwu » Tue May 29, 2012 7:02 pm

Hi everyone,

I'm doing my first lejOS robot (floor cleaning robot :) ) at the moment and have some (emedded) programming experience.

I wonder wether it has any speed/space advantages to use byte instead of integer in lejOS.
In the provided classes it seems that only integers are used (even if not required). So far I used byte where possible, but this requires a cast when using constants which is a bit anoying.
Is all the computation and storing done in integers anyway or does using byte gives an advantage?

And if I'm already asking about datatypes: am I right that there's no hardware floatingpoint unit available? Or, the other way around, is float "bad" (as in many embedded systems)?

Thanks,
Titiwu
User avatar
titiwu
New User
 
Posts: 3
Joined: Tue May 29, 2012 6:49 pm

Re: Is byte "better" than integer?

Postby gloomyandy » Tue May 29, 2012 8:03 pm

As is often the case the answer is it depends.... For arithmetic operations al data types smaller than int are converted to int before the operation is performed see the VM spec here:
http://docs.oracle.com/javase/specs/jvm ... html#16021
When stored on the stack (so a local variable) a byte will always occupy a stack word (which is 32 bits), so typically there is no advantage in terms of space or time to using bytes for locals or for calculations. However when values are stored in objects or in arrays then the data objects are packed so an array of 10 bytes will occupy 10 bytes (plus some object overhead), while an array of 10 shorts will require 20 bytes and and array of ints 40 bytes.

The Arm processor in the AT91 (the chip used in the NXT), contains no FP unit. However because of the overheads of interpreting the Java byte code floating point operations are not as much slower than integer operations as you may expect. The following post contains some basic tests that you may find of interest:
viewtopic.php?f=7&t=1685&p=7917&hilit=benchmark#p7917

So what would I conclude well, there is an old software rule about avoiding premature optimization and in this case by adding lots of byte operations you are trying to optimize things when there may not be a problem. If you are new to leJOS and robotics I would concentrate on getting your code to work using the simplest clearest coding approach and only worry about space and performance issues when you actually hit them or can clearly forse them (so for instance if you need to store 32000 table entries then using an int array on a 64Kb machine is almost certainly not going to work)...

Good luck

Andy
User avatar
gloomyandy
leJOS Team Member
 
Posts: 3881
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Re: Is byte "better" than integer?

Postby titiwu » Wed May 30, 2012 4:48 pm

Thanks for your complete answer and the links!
So far I've prgrammed Java for on Desktop and C on 8-bit microcontrollers (+ some other stuff) but no "embedded Java".
So I was used to take the fitting datatype as you probably would do in C (especially on a 8-bit controller).

My code is already working :wink: (not yet complete), but I'll replace most of the bytes I think.

By the way, it's based on a PetriNet Engine, I'll present it in the forum once its a bit more developped.

Thank you once again!
User avatar
titiwu
New User
 
Posts: 3
Joined: Tue May 29, 2012 6:49 pm


Return to NXJ Software

Who is online

Users browsing this forum: No registered users and 2 guests

more stuff