Created
February 29, 2012 15:17
-
-
Save tksmaru/1941492 to your computer and use it in GitHub Desktop.
Testing extended for-loop
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
old : 10times : 1msec | |
old : 100times : 7msec | |
old : 1000times : 39msec | |
old : 10000times : 44msec | |
old : 100000times : 374msec | |
old : 500000times : 1814msec | |
extended : 10times : 0msec | |
extended : 100times : 3msec | |
extended : 1000times : 22msec | |
extended : 10000times : 52msec | |
extended : 100000times : 544msec | |
extended : 500000times : 2488msec | |
old : 10times : 1msec | |
old : 100times : 0msec | |
old : 1000times : 3msec | |
old : 10000times : 31msec | |
old : 100000times : 326msec | |
old : 500000times : 1810msec | |
extended : 10times : 0msec | |
extended : 100times : 0msec | |
extended : 1000times : 3msec | |
extended : 10000times : 33msec | |
extended : 100000times : 387msec | |
extended : 500000times : 2171msec | |
old : 10times : 0msec | |
old : 100times : 0msec | |
old : 1000times : 2msec | |
old : 10000times : 22msec | |
old : 100000times : 294msec | |
old : 500000times : 1747msec | |
extended : 10times : 1msec | |
extended : 100times : 0msec | |
extended : 1000times : 3msec | |
extended : 10000times : 32msec | |
extended : 100000times : 373msec | |
extended : 500000times : 2167msec | |
old : 10times : 0msec | |
old : 100times : 0msec | |
old : 1000times : 2msec | |
old : 10000times : 23msec | |
old : 100000times : 345msec | |
old : 500000times : 1733msec | |
extended : 10times : 0msec | |
extended : 100times : 1msec | |
extended : 1000times : 3msec | |
extended : 10000times : 38msec | |
extended : 100000times : 392msec | |
extended : 500000times : 2170msec | |
old : 10times : 0msec | |
old : 100times : 0msec | |
old : 1000times : 2msec | |
old : 10000times : 22msec | |
old : 100000times : 302msec | |
old : 500000times : 1794msec | |
extended : 10times : 0msec | |
extended : 100times : 0msec | |
extended : 1000times : 3msec | |
extended : 10000times : 32msec | |
extended : 100000times : 390msec | |
extended : 500000times : 2376msec |
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
import java.util.ArrayList; | |
import java.util.List; | |
/** | |
* 拡張for文のテスト | |
* | |
* @author tksmaru | |
* | |
*/ | |
public class TestExtendedFor { | |
public static void main(String[] args) { | |
TestExtendedFor tester = new TestExtendedFor(); | |
// 最適化の恩恵を見てみるために5回繰り返してみる | |
for (int i = 0; i < 5; i++) { | |
tester.oldFor(tester.setUp(10)); | |
tester.oldFor(tester.setUp(100)); | |
tester.oldFor(tester.setUp(1000)); | |
tester.oldFor(tester.setUp(10000)); | |
tester.oldFor(tester.setUp(100000)); | |
tester.oldFor(tester.setUp(500000)); | |
tester.extendedFor(tester.setUp(10)); | |
tester.extendedFor(tester.setUp(100)); | |
tester.extendedFor(tester.setUp(1000)); | |
tester.extendedFor(tester.setUp(10000)); | |
tester.extendedFor(tester.setUp(100000)); | |
tester.extendedFor(tester.setUp(500000)); | |
} | |
} | |
/** | |
* 要素がloop数のListを生成します。 | |
* | |
* @param loop | |
* @return | |
*/ | |
public List<String> setUp(int loop) { | |
List<String> list = new ArrayList<String>(); | |
for (int i = 0; i < loop; i++) { | |
list.add(String.valueOf(i)); | |
} | |
return list; | |
} | |
/** | |
* 拡張for文でStringBufferの文字列連結を行う。 | |
* | |
* @param list | |
*/ | |
public void extendedFor(List<String> list) { | |
long start = System.currentTimeMillis(); | |
// 100回試行 | |
for (int i = 0; i < 100; i++) { | |
StringBuffer buff = new StringBuffer(); | |
for (String name : list) { | |
buff.append(name); | |
} | |
} | |
long fin = System.currentTimeMillis(); | |
System.out.println("extended : " + list.size() + "times : " | |
+ (fin - start) + "msec"); | |
} | |
/** | |
* 既存のfor文でStringBufferの文字列連結を行う。 | |
* | |
* @param list | |
*/ | |
public void oldFor(List<String> list) { | |
long start = System.currentTimeMillis(); | |
// 100回試行 | |
for (int i = 0; i < 100; i++) { | |
StringBuffer buff = new StringBuffer(); | |
for (int j = 0; j < list.size(); j++) { | |
buff.append(list.get(j)); | |
} | |
} | |
long fin = System.currentTimeMillis(); | |
System.out.println("old : " + list.size() + "times : " + (fin - start) | |
+ "msec"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment