Skip to content

Long2ObjectOpenHashMap is slower than HashMap in java.util #347

Closed
@gengjun-git

Description

@gengjun-git

This is my test code:

        final long TOTAL = 5000000L;
        Map<Long, Foo> map = new HashMap<>();
        // Map<Long, Foo> map = new Long2ObjectOpenHashMap<>();
        long maxW = -1L;
        long totalW = 0;
        long maxR = -1L;
        long totalR = 0;
        List<Long> wUsed = new ArrayList<>((int)TOTAL);
        List<Long> rUsed = new ArrayList<>((int)TOTAL);
        for (long i = 0; i < TOTAL; i++) {
            long start = System.nanoTime();
            Foo foo = new Foo();
            map.put(i, foo);
            long used = System.nanoTime() - start;
            maxW = Math.max(maxW, used);
            totalW += used;
            wUsed.add(used);
        }
        for (long i = 0; i < TOTAL; i++) {
            long start = System.nanoTime();
            map.get(i);
            long used = System.nanoTime() - start;
            maxR = Math.max(maxR, used);
            totalR += used;
            rUsed.add(used);
        }
        wUsed.sort(Comparator.reverseOrder());
        rUsed.sort(Comparator.reverseOrder());
        System.out.println("totalW: " + totalW + ", totalR: " + totalR
                + ", maxW: " + maxW + ", maxR: " + maxR
                + ", avgW: " + totalW / TOTAL + ", avgR: " + totalR / TOTAL
                + ", p99W: " + wUsed.get(50000) + ", p99R: " + rUsed.get(50000));

The result is

java.util.HashMap:
totalW: 663967578, totalR: 422418856, maxW: 63271855, maxR: 42850847, avgW: 132, avgR: 84, p99W: 137, p99R: 291
totalW: 651655688, totalR: 290515682, maxW: 56610865, maxR: 6989790, avgW: 130, avgR: 58, p99W: 135, p99R: 239
totalW: 700310638, totalR: 267878485, maxW: 53654800, maxR: 7820550, avgW: 140, avgR: 53, p99W: 146, p99R: 207
Long2ObjectOpenHashMap:
totalW: 1205439864, totalR: 736233534, maxW: 82863284, maxR: 373600, avgW: 241, avgR: 147, p99W: 359, p99R: 333
totalW: 1168520211, totalR: 789453626, maxW: 104233710, maxR: 1274739, avgW: 233, avgR: 157, p99W: 360, p99R: 352
totalW: 1142117792, totalR: 739646587, maxW: 109271044, maxR: 85790, avgW: 228, avgR: 147, p99W: 348, p99R: 336

jdk version is:

openjdk version "17.0.14" 2025-01-21
OpenJDK Runtime Environment Temurin-17.0.14+7 (build 17.0.14+7)
OpenJDK 64-Bit Server VM Temurin-17.0.14+7 (build 17.0.14+7, mixed mode)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions