Skip to content

Instantly share code, notes, and snippets.

@seut
Created November 21, 2018 10:05
Show Gist options
  • Save seut/cac2eab1c68eda387d8c399f211aa707 to your computer and use it in GitHub Desktop.
Save seut/cac2eab1c68eda387d8c399f211aa707 to your computer and use it in GitHub Desktop.
InitCapBenchmark
/*
* Licensed to Crate under one or more contributor license agreements.
* See the NOTICE file distributed with this work for additional
* information regarding copyright ownership. Crate licenses this file
* to you under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License. You may
* obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*
* However, if you have executed another commercial license agreement
* with Crate these terms will supersede the license and you may use the
* software solely pursuant to the terms of the relevant commercial
* agreement.
*/
package io.crate;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.State;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
@State(Scope.Benchmark)
public class InitCapBenchmark {
private static final String TEST_VALUE = "One morning, when Gregor Samsa woke from troubled dreams, he found " +
"himself transformed in his bed into a horrible vermin. He lay on his " +
"armour-like back, and if he lifted his head a little he could see his " +
"brown belly, slightly domed and divided by arches into stiff sections. " +
"The bedding was hardly able to cover it and seemed ready to slide off " +
"any moment. His many legs, pitifully thin compared with the size of the " +
"rest of him, waved about helplessly as he looked. " +
"\"What's happened to me?\" he thought. It wasn't a dream. His room, " +
"a proper human room although a little too small, lay peacefully between " +
"its four familiar walls. A collection of textile samples lay spread out " +
"on the table - Samsa was a travelling salesman - and above it there hung " +
"a picture that he had recently cut out of an illustrated magazine and " +
"housed in a nice, gilded frame. It showed a lady fitted out with a fur " +
"hat and fur boa who sat upright, raising a heavy fur muff that covered " +
"the whole of her lower arm towards the viewer. Gregor then turned to " +
"look out the window at the dull weather.";
private static final String SPLIT_STRING = " ";
private static String toCapital(String val) {
if (val.length() == 0) {
return "";
}
char[] chars = new char[val.length()];
boolean wordStarts = true;
for (int cp, i = 0, j = 0; i < val.length(); i += Character.charCount(cp)) {
cp = val.codePointAt(i);
if (Character.isSpaceChar(cp)) {
chars[j++] = val.charAt(i);
wordStarts = true;
continue;
}
if (wordStarts) {
chars[j++] = (char) Character.toUpperCase(cp);
wordStarts = false;
} else {
chars[j++] = (char) Character.toLowerCase(cp);
}
}
return new String(chars);
}
private static String toCapitalStream(String val) {
Stream<String> tokenStream = Arrays.stream(val.trim().split(SPLIT_STRING));
return tokenStream
.map(InitCapBenchmark::toTitleCase)
.collect(Collectors.joining(SPLIT_STRING));
}
private static String toTitleCase(String word) {
if (word.length() == 0)
return "";
char[] chars = word.toCharArray();
chars[0] = Character.toUpperCase(chars[0]);
for (int i = 1; i < chars.length; i++)
chars[i] = Character.toLowerCase(chars[i]);
return String.copyValueOf(chars);
}
@Benchmark
public void testCapitalizeByToken() {
for (int i = 0; i < 1000; i++) {
toCapitalStream(TEST_VALUE);
}
}
@Benchmark
public void testCapitalize() {
for (int i = 0; i < 1000; i++) {
toCapital(TEST_VALUE);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment