字符串拼接性能对比(Double.compare存一个类最快,不要存String)

package com.example.testsb.testNull;

public class Main {

    public static void main(String[] args) {
        final int n = 100000;

        long start = 0;

        start = System.currentTimeMillis();
        for (int i = 0; i < n; i++) {
            getKey(i, i, i, i, i, 1);
        }
        System.out.println(System.currentTimeMillis() - start);

        start = System.currentTimeMillis();
        for (int i = 0; i < n; i++) {
            getKey(i, i, i, i, i, 2);
        }
        System.out.println(System.currentTimeMillis() - start);

        start = System.currentTimeMillis();
        for (int i = 0; i < n; i++) {
            getKey(i, i, i, i, i, 3);
        }
        System.out.println(System.currentTimeMillis() - start);

        start = System.currentTimeMillis();
        for (int i = 0; i < n; i++) {
            getKey(i, i, i, i, i, 4);
        }
        System.out.println(System.currentTimeMillis() - start);

    }

    public static String getKey(int attributeType, double n1, double n2, double n3, double n4, int type) {
        switch (type) {
            case 1:
                return new StringBuilder().append(attributeType).append(n1).append(n2).append(n3).append(n4).toString();
            case 2:
                return String.valueOf(attributeType) + n1 + n2 + n3 + n4;
            case 3:
                return String.format("%d-%s-%s-%s-%s", attributeType, n1, n2, n3, n4);
            case 4:
                return new StringBuffer().append(attributeType).append(n1).append(n2).append(n3).append(n4).toString();
        }

        return "";
    }
}

/*
74
138
853
64
 */

因此推荐: new StringBuilder().append(x)....toString()方式

package com.example.testsb.testEqual;

import java.util.Objects;
import java.util.concurrent.ThreadLocalRandom;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class Main {

    public static void main(String[] args) {
        //        f(false);
        //        f(false);
        //        f(false);
        f(true);
    }


    public static void f(boolean b) {
        int N = 10_000_000;

        int attributeType;
        double n1;
        double n2;
        double n3;
        double n4;

        long startTime = System.currentTimeMillis();
        for (int i = 0; i < N; i++) {

            attributeType = ThreadLocalRandom.current().nextInt();
            n1 = ThreadLocalRandom.current().nextInt();
            n2 = ThreadLocalRandom.current().nextInt();
            n3 = ThreadLocalRandom.current().nextInt();
            n4 = ThreadLocalRandom.current().nextInt();

            String str1 = new StringBuilder().append(attributeType).append("-").append(n1).append("-").append(n2).append("-").append(n3).append("-")
                    .append(n4).toString();

            attributeType = ThreadLocalRandom.current().nextInt();
            n1 = ThreadLocalRandom.current().nextInt();
            n2 = ThreadLocalRandom.current().nextInt();
            n3 = ThreadLocalRandom.current().nextInt();
            n4 = ThreadLocalRandom.current().nextInt();

            String str2 = new StringBuilder().append(attributeType).append("-").append(n1).append("-").append(n2).append("-").append(n3).append("-")
                    .append(n4).toString();

            if (str1.equals(str2)) {
                log.error("equal");
            }
        }
        if (b) {
            log.error("-------{}", (System.currentTimeMillis() - startTime));
        }


        startTime = System.currentTimeMillis();
        for (int i = 0; i < N; i++) {
            attributeType = ThreadLocalRandom.current().nextInt();
            n1 = ThreadLocalRandom.current().nextInt();
            n2 = ThreadLocalRandom.current().nextInt();
            n3 = ThreadLocalRandom.current().nextInt();
            n4 = ThreadLocalRandom.current().nextInt();

            int hash1 = Objects.hash(attributeType, n1, n2, n3, n4);

            attributeType = ThreadLocalRandom.current().nextInt();
            n1 = ThreadLocalRandom.current().nextInt();
            n2 = ThreadLocalRandom.current().nextInt();
            n3 = ThreadLocalRandom.current().nextInt();
            n4 = ThreadLocalRandom.current().nextInt();

            int hash2 = Objects.hash(attributeType, n1, n2, n3, n4);

            if (hash1 == hash2) {
                log.error("equal2");
            }
        }

        if (b) {
            log.error("2222==={}", (System.currentTimeMillis() - startTime));

        }


        startTime = System.currentTimeMillis();
        for (int i = 0; i < N; i++) {
            int attributeType1 = ThreadLocalRandom.current().nextInt();
            double n11 = ThreadLocalRandom.current().nextInt();
            double n22 = ThreadLocalRandom.current().nextInt();
            double n33 = ThreadLocalRandom.current().nextInt();
            double n44 = ThreadLocalRandom.current().nextInt();


            attributeType = ThreadLocalRandom.current().nextInt();
            n1 = ThreadLocalRandom.current().nextInt();
            n2 = ThreadLocalRandom.current().nextInt();
            n3 = ThreadLocalRandom.current().nextInt();
            n4 = ThreadLocalRandom.current().nextInt();


            if (Double.compare(attributeType1, attributeType) == 0 && Double.compare(n11, n1) == 0 && Double.compare(n22, n2) == 0
                    && Double.compare(n33, n3) == 0 && Double.compare(n44, n4) == 0) {
                log.error("equal3");
            }
        }

        if (b) {
            log.error("-------3333==={}", (System.currentTimeMillis() - startTime));

        }
    }
}

/*
[2024-05-17 10:44:43.425] [main] [Main.java:53] ([ERROR][com.example.testsb.testEqual.Main] -------6402
[2024-05-17 10:44:44.121] [main] [Main.java:81] ([ERROR][com.example.testsb.testEqual.Main] 2222===690
[2024-05-17 10:44:44.201] [main] [Main.java:109] ([ERROR][com.example.testsb.testEqual.Main] -------3333===80
 */

  • 4
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值