Java string masking performance — Why you should stop worrying and rewrite everything in C

That brings us to the 3rd approach.

Method 33rd approachNo 3rd party dependencies here.

We convert string to an char array and the use Arrays.

fill to replace the chars.

Measuring performanceIn order to understand how these 3 behave and why one is better than the other, we need to look at how these behave under repeated calls.

We can do that by repeatedly calling these within a loop.

Integer.

MAX_VALUE is 2147483647.

Sufficiently large loop count.

With JDK 1.

8$ java -versionjava version "1.

8.

0_181"Java(TM) SE Runtime Environment (build 1.

8.

0_181-b13)Java HotSpot(TM) 64-Bit Server VM (build 25.

181-b13, mixed mode)Method 1 — takes about 10 minutes$ java Test1java Test1 585.

90s user 5.

60s system 101% cpu 9:45.

56 totalMethod 2 — takes about 2 minutes$ time java -cp commons-lang3-3.

9.

jar:.

Test2java -cp commons-lang3-3.

9.

jar:.

Test2 145.

74s user 1.

71s system 101% cpu 2:25.

72 totalMethod 3 — takes about 50 seconds$ time java Test3java Test3 51.

43s user 1.

01s system 100% cpu 52.

031 totalMassive performance differences with 3 different approaches.

Now next test surprised me.

Switched to JDK 9 and here are the results.

With JDK 9$ java -versionjava version "9.

0.

4"Java(TM) SE Runtime Environment (build 9.

0.

4+11)Java HotSpot(TM) 64-Bit Server VM (build 9.

0.

4+11, mixed mode)Method 1 — takes about 4 minutes$ time java Test1java Test1 270.

58s user 1.

60s system 101% cpu 4:29.

45 totalMethod 2 — takes about 2 minutes$ time java -cp commons-lang3-3.

9.

jar:.

Test2java -cp commons-lang3-3.

9.

jar:.

Test2 139.

44s user 1.

55s system 101% cpu 2:18.

31 totalMethod 3 — takes about 50 seconds$ time java Test3java Test3 50.

01s user 0.

62s system 102% cpu 49.

191 totalObservationsMethod 3 performance is unchanged across JDK versions.

JDK 9 is doing some massive optimizations on our 1st approach.

Essentially gives more than a 50% bump in performance.

However we should pay close attention to what implications our code might have on frequently executed code blocks on possibly high request processing use cases.

Now on to CAs I mentioned this is just for good measure.

C exampleC strings are terminated with the null character.

So we’re checking for null to make sure we’re at end of the string.

Resist the urge to use strlen here as it will slow down your programs massively.

Let’s see what that looks like,$ cc Test4.

c$ time .

/a.

out.

/a.

out 185.

07s user 0.

37s system 99% cpu 3:05.

75 total3 minutes!?.That’s because we have to let the compiler do its thang.

$ cc -O3 Test4.

c$ time .

/a.

out.

/a.

out 33.

52s user 0.

03s system 99% cpu 33.

577 total… and we’re down to 30 seconds!May be there are faster ways of doing things.

Hit me up on comments!.Full source here on github.

.

. More details

Leave a Reply