performance - How/why is Integer caching faster in Java? -


according java language specification, section 5.1.7, java caches integers -128 127 performance optimization.

so when compare a == b, a & b integers in caching range, returns true though they're different objects.

mechanically, performance benefits result caching? according this answer, "the purpose save memory, leads faster code due better cache efficiency." how lead faster code? how might use feature improve performance in real code write?

does have following method found in integercache class?

public static integer valueof(int i) {     assert integercache.high >= 127;     if (i >= integercache.low && <= integercache.high)         return integercache.cache[i + (-integercache.low)];     return new integer(i); } 

the performance benefit nothing ==. real reason cache allows valueof avoid creating lots of objects when same "small" integers boxed repeatedly. integer objects occupy space (at least 16 bytes). creating lots of them means garbage collector needs run more frequently.

my understanding java team did lot of analysis of real-world applications, , came conclusion integer cache worthwhile optimization.

(the reason nothing == cannot rely on == working integer objects. therefore, code ... without ensuring integers in specified range ... buggy. , if range check ensure numbers in-range, spend more on save using ==.)


"... leads faster code due better cache efficiency." how lead faster code?

the cache efficiency talking hardware level memory cache modern processors use deal mismatch between cpu , memory speeds. if application has 1 integer object representing number 1 , number appears frequently, chances memory addresses holding 1 object in (memory) caches increased. cache hits means faster code.


does have following method found in integercache class?

erm ... yes.


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -