2

why StringUtils.isEmtpy(s)? null:s is slower than !StringUtils.isEmtpy(s)? s:null? in my test, the later one is 1 millisecond faster than first one.

test:

   public static void main(String[] args) {
    List<String> list = new ArrayList<String>(Arrays.asList("a", "a", "a", "a", "a", null, "a"));
    Long start1 = System.nanoTime();
    for (String s : list
    ) {
        String s1 = isEmpty(s) ? null : s;

    }
    Long end1 = System.nanoTime();
    System.out.println("null in fisrt: " + (end1 - start1));
    Long start2 = System.nanoTime();
    for (String s : list
    ) {
        String s2 = !isEmpty(s) ? s : null;

    }
    Long end2 = System.nanoTime();
    System.out.println("null in second: " + (end2 - start2));
}

//output null in fisrt: 98600 null in second: 8200

WesleyHsiung
  • 351
  • 2
  • 13
  • 4
    Your testing methodology is flawed. Microbenchmarks are notoriously unreliable. "Figures often beguile me, particularly when I have the arranging of them myself; in which case the remark attributed to Disraeli would often apply with justice and force: 'There are three kinds of lies: lies, damned lies, and statistics.'" - Mark Twain – Elliott Frisch Jan 04 '19 at 04:30
  • This is not the way to test performance – Ryuzaki L Jan 04 '19 at 04:37
  • @Deadpool,would you please give more details? thanks – WesleyHsiung Jan 04 '19 at 04:43
  • 2
    check this https://stackoverflow.com/questions/447739/java-performance-testing – Ryuzaki L Jan 04 '19 at 04:50
  • if you want in both for loop just use the same way of `isEmpty()` method still you will see the time difference, which execution is complete depends on JVM loading ,cpu , etc.... – Ryuzaki L Jan 04 '19 at 04:51
  • 1
    See [How do I write a correct micro-benchmark in Java?](https://stackoverflow.com/q/504103/5221149) – Andreas Jan 04 '19 at 05:17
  • 1 millisecond diff is not issue, because it depends on your system resource load, jvm-warming and so on. I vote to close this question, because it is off topic, and has nothing with programming. – BSeitkazin Jan 04 '19 at 06:46

2 Answers2

0

Running the code as posted on my machine yields :

null in fisrt: 79347
null in second: 13027

Rearranging the code so "null is second" runs first :

null in second: 42239
null in fisrt: 5526

It clearly shows that you can't conclude which is faster from this test.

c0der
  • 18,467
  • 6
  • 33
  • 65
0

Firstly, Your methodology for calculating performance is flawed as stated in previous comments.

Usage of

String s1 = isEmpty(s) ? null : s;
String s2 = !isEmpty(s) ? s : null;

has really no effect on performance for any practical list size. The only overhead between the two LOC is the negation and branching accordingly.

Also, stop worrying of the same. The more you think of micro-optimising, you are almost every time wasting your time because the compiler is smart enough to perform the micro-optimizations itself.

Moreover hardware is getting cheaper day by day which can run programs unimaginably fast, but maintaining your codebase gets complex with every single line of code.

Trade your time to make the code more readable for others, IMO, you could have written like

String s1 = StringUtils.trimToNull(s);

and be done with it.

PS: Please don't compare trimToNull() with your code for performance and worry again!

Mohamed Anees A
  • 4,119
  • 1
  • 22
  • 35