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  
  Does this cause lack of Performance?  (Read 1932 times)
0 Members and 1 Guest are viewing this topic.
Offline Andre Lopes
« Posted 2013-10-12 05:37:21 »

I call those methods in every render();

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
 public void showHealthPotionsQuantity(int healthPotions) {   
       
        slotOne_Health.setText("" + healthPotions);
    }

    public void showManaPotionsQuantity(int manaPotions) {
        slotTwo_Mana.setText("" + manaPotions);
    }

    public void showDiamondsQuantity(int diamondsQuantity) {
        slotThree_Diamonds.setText("" + diamondsQuantity);
    }

    public void showArrowsQuantity(int normalArrows, int heavyArrows) {
        slotFour_ArrowNormal.setText("" + normalArrows);
        slotFive_HeavyArrow.setText("" + heavyArrows);

    }


Should i use StringBuffer/StringBuilder?
Online SHC
« Reply #1 - Posted 2013-10-12 05:55:12 »

I don't think it cause lack of performance. I use this every time. In this case, creating a stringbuffer every time causes much lag.

Offline Jeremy
« Reply #2 - Posted 2013-10-12 07:29:14 »

Noting that string are immutable (and IIRC the literal is not optimized out in that case) it could if you were working with a tightly packed loop (or one with a lot of iterations), probably not worth considering in most cases though.

Anyways, the problem I have with that code is it doesn't very well convey the intentions of the code to another person or to the VM which makes it harder to read and harder to optimize. You don't want to concatenate to a string, you want to convert an integer to a string.

The better way to do this is to use String.valueOf, or Integer.toString(i)

JevaEngine, Latest Playthrough (This demo is networked with a centralized server model)

http://www.youtube.com/watch?v=rWA8bajpVXg
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Danny02
« Reply #3 - Posted 2013-10-12 10:21:56 »

If you are so concerned just use toString then  Roll Eyes
Offline cylab

JGO Ninja


Medals: 38



« Reply #4 - Posted 2013-10-12 10:37:43 »

afaik primitives are autoboxed to be able to call toString on them.  Jeremys answer is best.

Mathias - I Know What [you] Did Last Summer!
Offline tberthel
« Reply #5 - Posted 2013-10-12 11:39:41 »

String.valueOf, or Integer.toString(i) are still slow, but acceptable for most PC use.

For high end performance you need something like I a have in my Allbinary Platform, but it cost money (1 dollar) to use each year.

https://github.com/AllBinary/AllBinary-Platform/blob/master/j2me/lib/BasicMathLibraryM/src/main/java/allbinary/logic/math/PrimitiveLongUtil.java

Basically you can't create strings or stringbuffers in your main game loop everyframe unless you don't care about performance.

Offline Danny02
« Reply #6 - Posted 2013-10-12 12:09:47 »

there he is again our old forum lunatic
Offline 65K
« Reply #7 - Posted 2013-10-12 12:30:56 »

Only create new Strings when you need to, namely when health and mana values change, store them and use them for rendering.

Offline Abuse

JGO Coder


Medals: 11


falling into the abyss of reality


« Reply #8 - Posted 2013-10-12 13:13:05 »

Use a profiler, don't optimize blindly.

Make Elite IV:Dangerous happen! Pledge your backing at KICKSTARTER here! https://dl.dropbox.com/u/54785909/EliteIVsmaller.png
Offline Cero
« Reply #9 - Posted 2013-10-12 16:05:17 »

It is he best practise to use a Stringbuilder with appends, to string, and flushes

however to quote myself
Quote
In most cases it is completely negligible.
Even on android.

I personally may do it properly only for one reason: it floods the memory overview when you profile and it gets annoying Grin

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Andre Lopes
« Reply #10 - Posted 2013-10-12 16:29:32 »

It is he best practise to use a Stringbuilder with appends, to string, and flushes

however to quote myself
Quote
In most cases it is completely negligible.
Even on android.

I personally may do it properly only for one reason: it floods the memory overview when you profile and it gets annoying Grin


Thanks for the replies guys!

Just a question then,

String.valueOf
is faster than

""  + 1

?
Offline cylab

JGO Ninja


Medals: 38



« Reply #11 - Posted 2013-10-12 16:33:46 »

should be.  but more importantly it does not create redundant temporary string objects that cause unwanted garbage collector runs. Also see: http://www.odi.ch/prog/design/newbies.php#3

Mathias - I Know What [you] Did Last Summer!
Offline Cero
« Reply #12 - Posted 2013-10-12 16:42:24 »

we are really talking about memory here to really performance, but yeah

"" + 1
is internally changed in to something like

String a = new String("");
String b = String.valueOf(1);
String c = a.concat(b);

so you can see a lot of call and unnecessary String objects for such a simple line D=

I recommend looking at the book "effective java" - just browse through and no shame if you dont get something, but the string are explained there and stuff
I also like it for many pitfalls like "System.out.println(01000);" or something like that, and I learned A LOT of primitive types and auto boxing and the sorts
rounding errors can really hurt you in unexpected ways if you have never heard about these kinds of things

Offline Andre Lopes
« Reply #13 - Posted 2013-10-12 16:46:09 »

should be.  but more importantly it does not create redundant temporary string objects that cause unwanted garbage collector runs. Also see: http://www.odi.ch/prog/design/newbies.php#3

Thanks for that! Already printing it and reading.

Ah, just more one question,

What about this then?

 public void showPlayerScore(int score) {
        this.score.setText("Score : " + String.valueOf(score));
    }


Is that correct?
Offline Andre Lopes
« Reply #14 - Posted 2013-10-12 16:51:10 »

we are really talking about memory here to really performance, but yeah

"" + 1
is internally changed in to something like

String a = new String("");
String b = String.valueOf(1);
String c = a.concat(b);

so you can see a lot of call and unnecessary String objects for such a simple line D=

I recommend looking at the book "effective java" - just browse through and no shame if you dont get something, but the string are explained there and stuff
I also like it for many pitfalls like "System.out.println(01000);" or something like that, and I learned A LOT of primitive types and auto boxing and the sorts
rounding errors can really hurt you in unexpected ways if you have never heard about these kinds of things

I love reading books.
But that book is from 2006, java was updated too much since from 2008.
http://www.amazon.com/Effective-Java-Edition-Joshua-Bloch/dp/0321356683/ref=sr_1_1?ie=UTF8&qid=1381589542&sr=8-1&keywords=effective+java

Is there any other book that explains performance/memory coding in java ? But its more actual?
Offline wreed12345

JGO Knight


Medals: 24
Projects: 2
Exp: 2 years


http://linebylinecoding.blogspot.com/


« Reply #15 - Posted 2013-10-12 16:51:40 »

I don't think performance should be worried about on something this small, but according to the posts above that seems like it will be better then how you had it

Offline Cero
« Reply #16 - Posted 2013-10-12 17:36:44 »

dont worry about the age, the 2nd edition still holds up. Joshua Bloch is one of the developers of Java.

This one is good too: Java Puzzlers: Traps, Pitfalls, and Corner Cases

Offline Andre Lopes
« Reply #17 - Posted 2013-10-12 17:41:35 »

dont worry about the age, the 2nd edition still holds up. Joshua Bloch is one of the developers of Java.

This one is good too: Java Puzzlers: Traps, Pitfalls, and Corner Cases

Hm, so its ok if i buy this book?
http://www.amazon.com/Effective-Java-Edition-Joshua-Bloch/dp/0321356683/ref=sr_1_1?ie=UTF8&qid=1381589542&sr=8-1&keywords=effective+java


Im also worried because of Java 8
Offline davedes
« Reply #18 - Posted 2013-10-12 17:57:27 »

On desktop it doesn't matter. On Android it can be a little costly on the GC if you are doing it in many places throughout your game loop. If you are targeting Android and want to do lots of string concats or int-to-strings in your game loop, then use LibGDX's StringBuffer which uses minimal allocations.

With that said though, it's best not to optimize blindly.

Offline Andre Lopes
« Reply #19 - Posted 2013-10-12 18:14:38 »

On desktop it doesn't matter. On Android it can be a little costly on the GC if you are doing it in many places throughout your game loop. If you are targeting Android and want to do lots of string concats or int-to-strings in your game loop, then use LibGDX's StringBuffer which uses minimal allocations.

With that said though, it's best not to optimize blindly.

Where can i learn more about analyzing Netbeans profile and Java Heap and those things? Im checking those things, but sometimes i dont understand whats going on....

Offline Cero
« Reply #20 - Posted 2013-10-12 23:30:32 »

never seen a guide or anything myself. might exist of course
just make some screenshots and ask here I suppose. But dont worry, it is confusing by nature =D

Offline philfrei
« Reply #21 - Posted 2013-10-13 04:06:22 »

Am reading a "classic" -- Brackeen's Developing Games in Java. Don't know how much of it is out of date (almost 10 years old!), but there is an entire chapter (chapter 16) devoted to optimization techniques. The very first rule of thumb will appeal to many here:

Quote
Don't optimize! First, attempt to create code that is easier to read, maintain, and modify.

There is a guide to using the HotSpot profiler. I haven't tried it yet. It uses the flag -Xprof. Maybe the jconsole.exe and jvisualvm.exe produce the equivalent reports? Both of those tools do have documents printed on how to use them. But, in contrast, the description/example in Brackeen is oriented to our game making needs.

Another BIG bonus is that he explains a technique for measuring and tuning the GC.

It would be outstanding, imho, if someone "reviewed" this chapter, gave a thumbs up/down on the current value of the various suggestions.

"Greetings my friends! We are all interested in the future, for that is where you and I are going to spend the rest of our lives!" -- The Amazing Criswell
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 (16 views)
2014-07-30 21:08:39

Riven (22 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 (30 views)
2014-07-23 21:07:15

Riven (31 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!