0

Which of these three variables and usages of it would be the fastest?

boolean booleanValue = true;
int booleanInt = 1;
byte booleanByte = 1;
char booleanCharI = 1;
char booleanCharS = '1';

Or anything else? (String booleanString = "true";)?

What are the advantages of each one? Is in this case code simplicity more important than performance?

Trick
  • 3,779
  • 12
  • 49
  • 76
  • 4
    For booleans use booleans, think about it logically, if anything was better than the boolean for booleans it would be used behind the scenes anyway – Richard Tingle Nov 05 '13 at 14:01
  • 4
    I wouldn't worry about performance and only write `boolean booleanValue = true;` to avoid confusions. – Alexis C. Nov 05 '13 at 14:01
  • 1
    `boolean` can be only `true` or `false`. Don't worry much about this kind of performance.. Even germs won't notice it. – Maroun Nov 05 '13 at 14:02
  • 1
    Guess it is about the same for all alternatives. But, since it is a boolean the first is far more clear than the others. – Henry Nov 05 '13 at 14:02
  • 2
    this is micro-micro-*micro*-optimization... – tckmn Nov 05 '13 at 14:02
  • 1
    The speed difference between these is going to be astronomically small (except perhaps a `String`), so stick with what is *most easily readable* (i.e. a `boolean`). – ajp15243 Nov 05 '13 at 14:02
  • 1
    Actually, why don't you benchmark it ? This will raise the question about what is "fast" in a boolean (read? write?) – benzonico Nov 05 '13 at 14:04
  • 1
    @benzonico Microbenchmarking on the JVM has 95% chance of producing deceiving results, unless done by an expert on the subject. – Marko Topolnik Nov 05 '13 at 14:21
  • @MarkoTopolnik For my curiosity and knowledge, can you provide some pointers to that ? – benzonico Nov 05 '13 at 14:28
  • @benzonico Google for "microbenchmarking java" and start learning :) You can try [this classic SO answer](http://stackoverflow.com/questions/504103/how-do-i-write-a-correct-micro-benchmark-in-java) as a good start. – Marko Topolnik Nov 05 '13 at 14:29
  • I can see I poke the beehive with this. Sorry :P – Trick Nov 06 '13 at 08:49

5 Answers5

3

You are in luck as the most obvious approach is also the fastest: a boolean variable. As an example, consider a slightly more involved boolean expression like the following:

c = a && b || o != null && o.equals(p)

where a, b, and c are your "boolean" variables and try rewriting that for the case where you are using int for boolean. You'll see that you are forced to introduce quite a bit more logic, potentially hurting performance and definitely hurting readability.

On a general note, unless you do nothing else but operations on primitive types in your complete codebase, you shall never be able to observe the difference in the various choices you are considering (except maybe for the string approach).

Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
1

Note that int, short, boolean and char are really the same data type inside JVM. For example, just decompile any class with you code containing boolean - javap -c . You'll see that boolean were convereted to integers. So boolean or int - really no difference in terms of performance.

Usage of Boolean would produce objects that wraps boolean values and it could be slower, but I beleive that JVM optimizes this, as Boolean is immutable objects, something like internization could be used. So I believe no performance difference too.

For real optimization you could use BitSet, or your own bit sets implementation, based on integers to store 32 boolean values inside one int.

  • What's in the bytecode is pretty irrelevant for the performance. Booleans are usually stored as bytes in objects, so there may be some cost if they're to be converted to ints (but I guess there's none, at least on [Intel](http://faydoc.tripod.com/cpu/movzx.htm)). – maaartinus Nov 05 '13 at 21:47
0

Can't say about fastest, but boolean booleanValue = true; is correct use of Boolean.

Dark Knight
  • 8,218
  • 4
  • 39
  • 58
  • 2
    In Java you should be careful about the distinction between `boolean` and `Boolean`. It is not at all a trivial difference. – Marko Topolnik Nov 05 '13 at 14:06
0

It is hard to think is there performance advantage in any of these cases. boolean true is more meaningful than int or char 1.

If you think about Boolean too there can be some performance advantage. There is performance advantage boolean over Boolean since boolean is a primitive type and use less memory.

Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115
0

Everything depends on usage, if you are going to have milion of boolean values then consider usage of BitSet . Using it will optimize memory usage. In the other hand all primitive type are represented in four or eight bytes and are set in one atomic operation.

Areo
  • 928
  • 5
  • 12
  • "one or two bytes"? Not quite. – Marko Topolnik Nov 05 '13 at 14:05
  • It's not that simple, either. The specification doesn't say anything about memory used by primitive types so a `byte` may well use just one byte, and a `byte[]` *is* specified to take one byte per element. – Marko Topolnik Nov 05 '13 at 14:19
  • @MarkoTopolnik Does it not? I thought I read that an int is a 32-bit signed two's complement number and a double is a 64-bit IEEE float. For example. – Ingo Nov 05 '13 at 14:22
  • @Ingo Indeed it is, *semantically*. That has no bearing on the actual number of bits occupied by it, except for the obvious lower bound. As a real-life example, OP's statement about "4 or 8 bytes" is probably just right for a primitive variable on a 32-bit JVM (I don't know if it's all 8 bytes for 64-bit). – Marko Topolnik Nov 05 '13 at 14:23
  • @MarkoTopolnik Strictly speaking you're right, but the specification is as it is for a reason: it fits contemporary hardware, and so we can assume that the JVM will use the native representation, if possible. – Ingo Nov 05 '13 at 14:25
  • 1
    @Ingo It fit contemporary hardware as of 15 years ago. Today the JVM's insistence on `long` and `double` operations not guaranteed to be atomic is outdated, for example. – Marko Topolnik Nov 05 '13 at 14:27
  • @Marko Not for my good, 6 or 7 year old laptop (where, incidentally, some Java programs run faster than on a much more recent i7-64bit desktop with hyperthreading and what not) – Ingo Nov 05 '13 at 14:29