Skip to content

Instantly share code, notes, and snippets.

@SidWagz
Last active December 30, 2017 17:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SidWagz/e41e836dec65ff24f78afdf8669e6420 to your computer and use it in GitHub Desktop.
Save SidWagz/e41e836dec65ff24f78afdf8669e6420 to your computer and use it in GitHub Desktop.
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class StringBuilderWithDeque {
public static void main(String[] args) {
long start, end;
int MAX_STR = Integer.parseInt(args[0]);
System.out.println("MAX_STR = " + MAX_STR);
StringBuilder build1 = new StringBuilder();
start = System.currentTimeMillis();
IntStream.range(0, MAX_STR)
.sequential()
.forEach(i -> build1.append(Integer.toString(i)));
String build1Out = build1.toString();
end = System.currentTimeMillis();
System.out.println("Final length = " + build1Out.length());
// System.out.println(build1Out.toString());
System.out.println("Case 1 : " + (end-start)/1000.0 + " sec");
StringBuilder build2 = new StringBuilder();
start = System.currentTimeMillis();
IntStream.range(0, MAX_STR)
.sequential()
.forEach(i -> build2.insert(0, Integer.toString(i)));
String build2Out = build2.toString();
end = System.currentTimeMillis();
// System.out.println(build2Out.toString());
System.out.println("Case 2 : " + (end-start)/1000.0 + " sec");
StringBuilder build3 = new StringBuilder();
start = System.currentTimeMillis();
IntStream.range(0, MAX_STR)
.sequential()
.forEach(i -> {
if (i%2 == 0) build3.append(Integer.toString(i)); else build3.insert(0, Integer.toString(i));
});
String build3Out = build3.toString();
end = System.currentTimeMillis();
// System.out.println(b3Out.toString());
System.out.println("Case 3 : " + (end-start)/1000.0 + " sec");
start = System.currentTimeMillis();
List<String> inorderList = new ArrayList<>();
IntStream.range(0, MAX_STR)
.sequential()
.forEach(i -> inorderList.add(Integer.toString(i)));
String listOut = inorderList.stream().collect(Collectors.joining(""));
end = System.currentTimeMillis();
// System.out.println(listOut.toString());
System.out.println("Case 4 : " + (end-start)/1000.0 + " sec");
start = System.currentTimeMillis();
Deque<String> deque = new ArrayDeque<>();
IntStream.range(0, MAX_STR)
.sequential()
.forEach(i -> {
if (i%2 == 0) deque.addLast(Integer.toString(i)); else deque.addFirst(Integer.toString(i));
});
String dequeOut = deque.stream().collect(Collectors.joining(""));
end = System.currentTimeMillis();
// System.out.println(dequeOut.toString());
System.out.println("Case 5 : " + (end-start)/1000.0 + " sec");
}
}
/**
*
* Outputs from different sample array sizes.
*
MAX_STR = 1000
Final length = 2890
Case 1 : 0.087 sec
Case 2 : 0.002 sec
Case 3 : 0.002 sec
Case 4 : 0.008 sec
Case 5 : 0.002 sec
MAX_STR = 5000
Final length = 18890
Case 1 : 0.084 sec
Case 2 : 0.007 sec
Case 3 : 0.005 sec
Case 4 : 0.012 sec
Case 5 : 0.004 sec
MAX_STR = 10000
Final length = 38890
Case 1 : 0.082 sec
Case 2 : 0.025 sec
Case 3 : 0.018 sec
Case 4 : 0.031 sec
Case 5 : 0.006 sec
MAX_STR = 20000
Final length = 88890
Case 1 : 0.086 sec
Case 2 : 0.09 sec
Case 3 : 0.046 sec
Case 4 : 0.023 sec
Case 5 : 0.01 sec
MAX_STR = 50000
Final length = 238890
Case 1 : 0.094 sec
Case 2 : 0.668 sec
Case 3 : 0.345 sec
Case 4 : 0.034 sec
Case 5 : 0.019 sec
MAX_STR = 100000
Final length = 488890
Case 1 : 0.111 sec
Case 2 : 3.058 sec
Case 3 : 1.498 sec
Case 4 : 0.049 sec
Case 5 : 0.026 sec
MAX_STR = 200000
Final length = 1088890
Case 1 : 0.123 sec
Case 2 : 13.394 sec
Case 3 : 6.714 sec
Case 4 : 0.272 sec
Case 5 : 0.029 sec
MAX_STR = 500000
Final length = 2888890
Case 1 : 0.153 sec
Case 2 : 127.874 sec
Case 3 : 65.331 sec
Case 4 : 0.461 sec
Case 5 : 0.181 sec
*
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment