Created
January 8, 2021 13:55
-
-
Save kurenaif/d5a2eac96f27f6245cbccab8122ac600 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.example; | |
public class Main { | |
public static void main(String[] args) { | |
var sb = new StringBuilder(); | |
for(int i=0;i<10000000;i++){ | |
sb.append("a"); | |
} | |
var base = new String(sb); | |
var aaaaa = base + "a"; | |
var aaaab = base + "b"; | |
var baaaa = "b" + base; | |
long dummy = 0; | |
// "aaa...aaa" v.s. "aaa...aab" | |
{ | |
long startTime = System.currentTimeMillis(); | |
for(int i=0;i<1000;i++) { | |
if (aaaaa.equals(aaaab)) { | |
dummy += 1; | |
} | |
} | |
long endTime = System.currentTimeMillis(); | |
System.out.println(endTime - startTime); // => 2901 | |
} | |
// "aaa...aaa" v.s. "baa...aaa" | |
{ | |
long startTime = System.currentTimeMillis(); | |
for(int i=0;i<1000;i++) { | |
if(aaaaa.equals(baaaa)){ | |
dummy += 1; | |
} | |
} | |
long endTime = System.currentTimeMillis(); | |
System.out.println(endTime - startTime); // => 0 | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment