I was just wondering about the efficency of the following two codes:
public void appendInput(String s, boolean isCommand) {
s = "\n" + (isCommand == true ? ">" : "") + s + "\n";
getConsoleInput().append(s);
//vs
getConsoleInput().append("\n" + (isCommand == true ? ">" : "") + s + "\n");
}
By my basic grasp of this language, would the second one be more efficent than the second variation because it does not create a new object (String
)? Or does it create a new String
when you contract two String
s? I think so. I just need someone who has a better grasp of Java than me.
So I decided to run some tests. This is what I came up with:
public class Main {
public static void main(String[] args) {
String s;
Boolean b;
long startTime;
long endTime;
long[] results = new long[4];
System.out.println("*ALL TESTS WILL BE LOOPED *1000");
sep();
System.out.println("METHOD 1;-- TEST 1 - STRING = \"TESTING \" && BOOLEAN = FALSE:");
s = "TESTING";
b = false; // change variables
startTime = System.currentTimeMillis(); //commence testing from here
for(int i = 0; i < 1000; i ++) {
s = "" + (b ? ">" : "") + s + "";
System.out.print(s);
}
endTime = System.currentTimeMillis();
results[0] = endTime - startTime;
System.out.println("Total execution time: " + results[0] + "ms");
sep();
//Next test
System.out.println("METHOD 1;-- TEST 2 - STRING = \"TESTING \" && BOOLEAN = TRUE:");
s = "TESTING";
b = false;
startTime = System.currentTimeMillis();
for(int i = 0; i < 1000; i ++) {
s = "" + (b ? ">" : "") + s + "";
System.out.print(s);
}
endTime = System.currentTimeMillis();
results[1] = endTime - startTime;
System.out.println("Total execution time: " + results[1] + "ms");
sep();
//Next test
System.out.println("METHOD 2;-- TEST 1 - STRING = \"TESTING \" && BOOLEAN = FALSE:");
s = "TESTING";
b = false; // change variables
startTime = System.currentTimeMillis(); //commence testing from here
for(int i = 0; i < 1000; i ++) {
System.out.print("" + (b == true ? ">" : "") + s + "");
}
endTime = System.currentTimeMillis();
results[2] = endTime - startTime;
System.out.println("Total execution time: " + results[2] + "ms");
sep();
//Next test
System.out.println("METHOD 2;-- TEST 2 - STRING = \"TESTING \" && BOOLEAN = TRUE:");
s = "TESTING";
b = false;
startTime = System.currentTimeMillis();
for(int i = 0; i < 1000; i ++) {
System.out.print("" + (b == true ? ">" : "") + s + "");
}
endTime = System.currentTimeMillis();
results[3] = endTime - startTime;
System.out.println("Total execution time: " + results[3] + "ms");
sep();
System.out.println("RESULTS:");
System.out.println("-----------------------");
String[] typesOfTests = {"METHOD 1 BOOLEAN = FALSE", "METHOD 1 BOOLEAN = TRUE ",
"METHOD 2 BOOLEAN = FALSE", "METHOD 2 BOOLEAN = TRUE "};
for(int i = 0; i < typesOfTests.length && i < results.length; i++) {
System.out.print(typesOfTests[i]);
System.out.print("\t");
System.out.println(results[i]);
}
}
private static void sep() {
System.out.println("====================================");
}
private void optOne(String s, boolean b) {
s = "\n" + (b == true ? ">" : "") + s + "\n";
System.out.println(s);
}
private void optTwo(String s, boolean b) {
System.out.println("\n" + (b == true ? ">" : "") + s + "\n");
}
}
And here are some results...
ALL TESTS WILL BE LOOPED *1000
====================================
METHOD 1;-- TEST 1 - STRING = "TESTING " && BOOLEAN = FALSE:
TESTING *1000 ...
Total execution time: 25ms
====================================
METHOD 1;-- TEST 2 - STRING = "TESTING " && BOOLEAN = TRUE:
TESTING *1000 ...
Total execution time: 17ms
====================================
METHOD 2;-- TEST 1 - STRING = "TESTING " && BOOLEAN = FALSE:
TESTING *1000 ...
Total execution time: 4ms
====================================
METHOD 2;-- TEST 2 - STRING = "TESTING " && BOOLEAN = TRUE:
TESTING *1000 ...
Total execution time: 8ms
====================================
RESULTS:
-----------------------
METHOD 1 | BOOLEAN = FALSE | 25
METHOD 1 | BOOLEAN = TRUE | 17
METHOD 2 | BOOLEAN = FALSE | 4
METHOD 2 | BOOLEAN = TRUE | 8
and some others (OK, OK, table only!)
METHOD 1 | BOOLEAN = FALSE | 19
METHOD 1 | BOOLEAN = TRUE | 10
METHOD 2 | BOOLEAN = FALSE | 5
METHOD 2 | BOOLEAN = TRUE | 5
METHOD 1 | BOOLEAN = FALSE | 18
METHOD 1 | BOOLEAN = TRUE | 11
METHOD 2 | BOOLEAN = FALSE | 5
METHOD 2 | BOOLEAN = TRUE | 4
METHOD 1 | BOOLEAN = FALSE | 20
METHOD 1 | BOOLEAN = TRUE | 16
METHOD 2 | BOOLEAN = FALSE | 6
METHOD 2 | BOOLEAN = TRUE | 4
Conclusion:
Method 1 (s = "" + (b ? ">" : "") + s + ""; System.out.print(s);
) is much faster than method 2 (System.out.print("" + (b == true ? ">" : "") + s + "");
) as there is no need to create a new String
object.
Another trend I have noticed is that the time increases by more than a third when the boolean
is false
, in both of the methods even though in method 1 it is more noticeable than in method 2. I have no idea why this is... Could anyone explain this to me?
TL;DR method 1 is slower as a new String
object needs to be created. Furthermore, when the boolean
is equal to false
, time taken to execute methods increases in both cases. Could you explain this to me?