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
*/