import java.util.*;
import java.lang.*;
import java.io.*;
public class Main {
private static int loop1(List<String> list) {
int charCount = 0;
for (int i = 0; i < list.size(); i++) {
charCount += list.get(i).length();
}
return charCount;
}
private static int loop2(List<String> list) {
int charCount = 0;
int listCount = list.size();
for (int i = 0; i < listCount; i++) {
charCount += list.get(i).length();
}
return charCount;
}
private static int loop3(List<String> list) {
int charCount = 0;
charCount += s.length();
}
return charCount;
}
public static void main
(String[] args
) { ArrayList<String> ls = new ArrayList<String>();
for (int j = 0; j < 1000000; j++) {
ls.add("one");
ls.add("two");
ls.add("three");
ls.add("four");
ls.add("five");
}
long startTime1
= System.
nanoTime(); int count1 = loop1(ls);
long elapsed1
= System.
nanoTime() - startTime1
;
long startTime2
= System.
nanoTime(); int count2 = loop2(ls);
long elapsed2
= System.
nanoTime() - startTime2
;
long startTime3
= System.
nanoTime(); int count3 = loop3(ls);
long elapsed3
= System.
nanoTime() - startTime3
;
System.
out.
println("Elapsed1: " + elapsed1
+ " ns"); System.
out.
println("Elapsed2: " + elapsed2
+ " ns"); System.
out.
println("Elapsed3: " + elapsed3
+ " ns"); }
}
CmltcG9ydCBqYXZhLnV0aWwuKjsKaW1wb3J0IGphdmEubGFuZy4qOwppbXBvcnQgamF2YS5pby4qOwoKcHVibGljIGNsYXNzIE1haW4gewoJCglwcml2YXRlIHN0YXRpYyBpbnQgbG9vcDEoTGlzdDxTdHJpbmc+IGxpc3QpIHsKICAgICAgICBpbnQgY2hhckNvdW50ID0gMDsKICAgICAgICBmb3IgKGludCBpID0gMDsgaSA8IGxpc3Quc2l6ZSgpOyBpKyspIHsKICAgICAgICAgICAgY2hhckNvdW50ICs9IGxpc3QuZ2V0KGkpLmxlbmd0aCgpOwogICAgICAgIH0KICAgICAgICByZXR1cm4gY2hhckNvdW50OwogICAgfQoKICAgIHByaXZhdGUgc3RhdGljIGludCBsb29wMihMaXN0PFN0cmluZz4gbGlzdCkgewogICAgICAgIGludCBjaGFyQ291bnQgPSAwOwogICAgICAgIGludCBsaXN0Q291bnQgPSBsaXN0LnNpemUoKTsKICAgICAgICBmb3IgKGludCBpID0gMDsgaSA8IGxpc3RDb3VudDsgaSsrKSB7CiAgICAgICAgICAgIGNoYXJDb3VudCArPSBsaXN0LmdldChpKS5sZW5ndGgoKTsKICAgICAgICB9CiAgICAgICAgcmV0dXJuIGNoYXJDb3VudDsKICAgIH0KCiAgICBwcml2YXRlIHN0YXRpYyBpbnQgbG9vcDMoTGlzdDxTdHJpbmc+IGxpc3QpIHsKICAgICAgICBpbnQgY2hhckNvdW50ID0gMDsKICAgICAgICBmb3IgKFN0cmluZyBzIDogbGlzdCkgewogICAgICAgICAgICBjaGFyQ291bnQgKz0gcy5sZW5ndGgoKTsKICAgICAgICB9CiAgICAgICAgcmV0dXJuIGNoYXJDb3VudDsKICAgIH0KCiAgICBwdWJsaWMgc3RhdGljIHZvaWQgbWFpbihTdHJpbmdbXSBhcmdzKSB7CiAgICAgICAgQXJyYXlMaXN0PFN0cmluZz4gbHMgPSBuZXcgQXJyYXlMaXN0PFN0cmluZz4oKTsKCiAgICAgICAgZm9yIChpbnQgaiA9IDA7IGogPCAxMDAwMDAwOyBqKyspIHsKICAgICAgICAgICAgbHMuYWRkKCJvbmUiKTsKICAgICAgICAgICAgbHMuYWRkKCJ0d28iKTsKICAgICAgICAgICAgbHMuYWRkKCJ0aHJlZSIpOwogICAgICAgICAgICBscy5hZGQoImZvdXIiKTsKICAgICAgICAgICAgbHMuYWRkKCJmaXZlIik7CiAgICAgICAgfQoKICAgICAgICBsb25nIHN0YXJ0VGltZTEgPSBTeXN0ZW0ubmFub1RpbWUoKTsKICAgICAgICBpbnQgY291bnQxID0gbG9vcDEobHMpOwogICAgICAgIGxvbmcgZWxhcHNlZDEgPSBTeXN0ZW0ubmFub1RpbWUoKSAtIHN0YXJ0VGltZTE7CgogICAgICAgIGxvbmcgc3RhcnRUaW1lMiA9IFN5c3RlbS5uYW5vVGltZSgpOwogICAgICAgIGludCBjb3VudDIgPSBsb29wM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