Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (536)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Is is faster to just.... not check some condition?  (Read 1710 times)
0 Members and 1 Guest are viewing this topic.
Offline nhmllr

Senior Member


Medals: 1
Projects: 3


slow and steady...


« Posted 2012-07-02 19:14:06 »

Here's a stupid scenario to illustrate a problem
Let's say that sometimes some boolean variable "condition" is true
Now, we have some object, and one of this object's "jobs" is to make condition false is condition is ever true.

Is there a point to doing
1  
2  
3  
if(condition){
     condition = false;
}


or is simply doing
1  
condition = false;

faster? (does it change, depending on about how often "condition" is true?)

Does looking up the state of a variable take less time than simply setting it?
This is just a miniscule efficiency problem that i've always wondered about.

RELATED: do certain "lookups" take longer than others? Like, does checking a boolean take less time than checking an int, and does that take less time than checking if an object is some subclass? By how much?

Thanks!
Offline matheus23

JGO Kernel


Medals: 106
Projects: 3


You think about my Avatar right now!


« Reply #1 - Posted 2012-07-02 19:20:38 »

80% sure, both will have the same performance.
Actually
1  
condition = false
is faster. But since our nice JVM optimizes code, it optimizes
1  
 if (condition) condition = false; 
to the above. You could simply think of the lookup being about as fast as the "set".

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Offline Rorkien
« Reply #2 - Posted 2012-07-02 20:08:09 »

Checking and changing 1 bit of data? Im pretty sure it is so insignificant and fast that it turns out to be unmesurable


Also, you can do it using ternary, but it is more confusing and you can't omit the second statement :/
1  
condition = condition ? !condition : condition;
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Online ags1

JGO Ninja


Medals: 46
Projects: 2
Exp: 5 years


Make code not war!


« Reply #3 - Posted 2012-07-02 20:55:22 »

Booleans are not represented by one bit by the JVM - if I recall correctly a boolean value might take up to 4 bytes of memory. I think checking for true and then setting to false cannot ever be faster - you have to read the value, make a comparison then write the value. I think there might be something wrong with the flow if you are setting the field a zillion times...

Offline Cero
« Reply #4 - Posted 2012-07-02 21:04:22 »

I think in Java booleans can even be as big as 32byte.

But nevertheless

so insignificant and fast that it turns out to be unmesurable
This should be true. Like measuring the weight of one or ten electrons =D

Offline matheus23

JGO Kernel


Medals: 106
Projects: 3


You think about my Avatar right now!


« Reply #5 - Posted 2012-07-02 21:07:46 »

I think in Java booleans can even be as big as 32byte.
Are we talking about the same boolean here? Do you actually mean the java.lang.Boolean (which of course can get 32 bytes big... all the JVM overhead :O ) or are we talking about the generic-type-boolean, which (I'm pretty sure) takes 1 byte ?

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Online ags1

JGO Ninja


Medals: 46
Projects: 2
Exp: 5 years


Make code not war!


« Reply #6 - Posted 2012-07-02 21:13:01 »

Well, this guy claims 32 bits (not bytes), which would match my 4 byte claim:

http://weblogs.java.net/blog/dwalend/archive/2007/11/the_thing_about.html

Offline Rorkien
« Reply #7 - Posted 2012-07-02 21:26:48 »

Well, this guy claims 32 bits (not bytes), which would match my 4 byte claim:

http://weblogs.java.net/blog/dwalend/archive/2007/11/the_thing_about.html

Good find!
And now i also know that using an array of booleans is less efficient than using N booleans
Offline matheus23

JGO Kernel


Medals: 106
Projects: 3


You think about my Avatar right now!


« Reply #8 - Posted 2012-07-02 21:36:29 »

Well, this guy claims 32 bits (not bytes), which would match my 4 byte claim:

http://weblogs.java.net/blog/dwalend/archive/2007/11/the_thing_about.html

Good find!
And now i also know that using an array of booleans is less efficient than using N booleans
Nope... Looks like booleans inside the STACK are 32 bits of data. But inside Arrays and classes they only take 8 bits (as I expected). That is rather "normal", because C and some other languages also use 8 bits per boolean. In C++ there is the nice vector class, which has a implementation of boolean arrays taking only 1 bit per boolean. Access and writing is a bit slower, but I think thats quite okey for 8x less data Wink

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Online ags1

JGO Ninja


Medals: 46
Projects: 2
Exp: 5 years


Make code not war!


« Reply #9 - Posted 2012-07-02 22:45:17 »

Well, my point was that it wasn't one bit... "one to four bytes" would be the accurate statement I guess. You can always make your own bitmask if it really matters.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline sproingie

JGO Kernel


Medals: 202



« Reply #10 - Posted 2012-07-03 02:50:56 »

Also, you can do it using ternary, but it is more confusing and you can't omit the second statement :/
1  
condition = condition ? !condition : condition;


That's a rather roundabout way of saying:
1  
condition = false;

Offline ra4king

JGO Kernel


Medals: 340
Projects: 2
Exp: 5 years


I'm the King!


« Reply #11 - Posted 2012-07-03 03:36:43 »

This "optimization" is so evil that just looking at it makes me sin.

Offline Roquen
« Reply #12 - Posted 2012-07-03 13:26:30 »

But it is near optimal.  Assuming writing obtuse code is the goal.
Offline Rorkien
« Reply #13 - Posted 2012-07-03 19:30:07 »

Also, you can do it using ternary, but it is more confusing and you can't omit the second statement :/
1  
condition = condition ? !condition : condition;


That's a rather roundabout way of saying:
1  
condition = false;



The entire line looks dumb either way
It is called brute force, when you don't want to lose time debugging, so you just maul a value on the variable (Don't do this at home!):

1  
2  
3  
4  
5  
6  
7  
if (var == 40) {
    doSomething(var);
}
else {
    var = 40;
    doSomething(var);
}


It is on the same class as the fair dice roll
Offline Roquen
« Reply #14 - Posted 2012-07-03 19:49:50 »

That doesn't work either: transformed into the else statement in the same way as loosing the original if of OP.
Offline Cero
« Reply #15 - Posted 2012-07-03 21:19:32 »

ah yes I meant 32bits

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

CogWheelz (9 views)
2014-07-30 21:08:39

Riven (20 views)
2014-07-29 18:09:19

Riven (14 views)
2014-07-29 18:08:52

Dwinin (12 views)
2014-07-29 10:59:34

E.R. Fleming (32 views)
2014-07-29 03:07:13

E.R. Fleming (12 views)
2014-07-29 03:06:25

pw (42 views)
2014-07-24 01:59:36

Riven (42 views)
2014-07-23 21:16:32

Riven (29 views)
2014-07-23 21:07:15

Riven (30 views)
2014-07-23 20:56:16
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!