Last active
November 16, 2017 12:37
-
-
Save snicmakino/4580d3e7cc67ce7106725e7e2b8fdff9 to your computer and use it in GitHub Desktop.
キャメルケース→スネークケース変換処理を、正規表現、Stringの結合、StringBuilderで比較
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
// キャメルケース→スネークケース変換処理 | |
public class Main { | |
public static void main(String[] args) throws Exception { | |
String target = "snicMakinoTestCase"; | |
System.out.println("テストケース:" + target + "\n"); | |
Main main = new Main(); | |
main.timeMeasure( | |
"正規表現", | |
target, | |
(str) -> { | |
return str.replaceAll("([A-Z])", "_$1").toLowerCase(); | |
}); | |
main.timeMeasure( | |
"くるくる", | |
target, | |
(str) -> { | |
String result = ""; | |
for (char c : str.toCharArray()) { | |
if (Character.isUpperCase(c)) { | |
result += "_" + Character.toString(Character.toLowerCase(c)); | |
continue; | |
} | |
result += Character.toString(c); | |
} | |
return result; | |
}); | |
main.timeMeasure( | |
"くるくる2", | |
target, | |
(str) -> { | |
StringBuilder result = new StringBuilder(); | |
for (char c : str.toCharArray()) { | |
if (Character.isUpperCase(c)) { | |
result.append('_').append(Character.toLowerCase(c)); | |
continue; | |
} | |
result.append(c); | |
} | |
return result.toString(); | |
}); | |
} | |
public void timeMeasure(String name, String target, CamelToSnake camelToSnake) { | |
System.out.println("処理名:" + name); | |
System.out.println("処理結果:" + camelToSnake.execute(target)); | |
long start = System.currentTimeMillis(); | |
for (int i = 0; i < 10000; i++) { | |
camelToSnake.execute(target); | |
} | |
long end = System.currentTimeMillis(); | |
System.out.printf("実行時間:%d\n\n", end - start); | |
} | |
public interface CamelToSnake { | |
public String execute(String arg); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment