java - Why is map.values().stream() much slower than Array.stream(array) -
creating 2 constructs university second semester computer science count words in text. 1 implementation uses array word-objects save word string , frequency int. other uses hashmap word key , frequency value. function "totalwords" should return sum of frequency.
in hashmap variant:
return _map.values().stream().reduce(0, (a, b) -> + b);
in array variant:
return arrays.stream(_words) .map((word) -> word != null ? word.count() : 0) .reduce(0, (a, b) -> + b);
my problem is: in junit test short test text array variant need approximately 0.001s , map variant needs 0.040s , not understand why map need more time. has explanation , maybe better solution?
one of reason iterating hashmap
can slower array
, reason locality
. computation bottleneck of modern processor dominated memory access, , that's why cache
used. array
stores data in contiguous chunk of memory, means when swap chunk of memory cache, more using in cache, or cache hits
, cache likes contiguous memory of data. on other hand, each element of hashmap
stored in different place in memory, when traverse hashmap
, lot of cache misses
, end swapping data in , out of cache time, dramatically slows down program.
although actual implementation of hashmap
in optimized way such data in memory clustered together, in case, (@radiodef)since hashmap
uses sort of linked list
, each element of hashmap
contains pointers, hashmap
consumes more memory array
, more memory means more cache misses
, more page faults
, hashmap
in general slower array
.
Comments
Post a Comment