java - Efficiency of Random Method -
i read not want make new instances when unnecessary. not sure if understand saying. problem repeat of in logcat:
08-09 17:12:11.300: d/dalvikvm(19620): gc_for_alloc freed 2281k, 23% free 9365k/12048k, paused 12ms, total 12ms
so better have method:
public int rand(int i) { int rand = new random().nextint(i); return rand; }
than have method:
public int rand(int i) { return new random().nextint(i); }
if calling rand() lot? why or why not? helping newb!
do this:
class ... { private final random dice = new random(); public int rand(int i) { return dice.nextint(i); } }
that way, calling new random()
once, instead of every time generate number.
Comments
Post a Comment