0

Which piece of code has better performance?

Pseudocode:
1)
prvate String doSth(String s) {
...
 return s.substring(0, Math.min(s.length(), constparam));
}

2)
prvate String doSth(String s) {
 if (s.length() > constparam) {
  return s.substring(0, constparam);
 }
 return s;
}

In most cases (99%) - s.length < constparam. This method is invoked 20-200 times per second. Which solution (and why) would have a better performance? Will it be a significant impact?

Max
  • 33
  • 3
  • 2
    Why don't you run some performance tests and find out for yourself? This (and the duplicate) might help: https://stackoverflow.com/questions/447739/java-performance-testing – lucasvw Jun 20 '17 at 17:34
  • 2
    Once JIT has analyzed the code, there is likely little to no difference, and if you only do this 200 times per second, you will likely not notice any difference if there is one. **Beware premature optimization**, i.e. only "optimize" the code if you measure a problem, otherwise use the version of code that seems more logical. – Andreas Jun 20 '17 at 17:54
  • You gonna need this for sure https://stackoverflow.com/questions/504103/how-do-i-write-a-correct-micro-benchmark-in-java – Andrey Cheboksarov Jun 20 '17 at 19:42

5 Answers5

0

Let's look at what each one does:

1 always finds the lower of two values and always calls substring returning a new String.

2 always compares two values and sometimes calls sub string so sometimes creates a new String.

So 2, because some of the time it will do less work and create less objects.

MartinByers
  • 1,240
  • 1
  • 9
  • 15
0

If s.length < constparam for most of the cases, case 2 will be faster as substring() operation need not to be done for most of the cases.

Nabin Bhandari
  • 15,949
  • 6
  • 45
  • 59
0

Function substring with arguments (0, length) returns the string unmodified.

The difference is checking if s.length() > constparam, but basically it's what Math.min does.

So im my opinion, there's almost no performance differences, assuming substring invocation takes much more time than this conditional or Math.min, or even without this assumption.

Adrian Adamczyk
  • 3,000
  • 5
  • 25
  • 41
0
public static int min(int a, int b) {
    return (a <= b) ? a : b;
}

From Math. I would run a JMH test, and I would say that the two presented solution won't show statistically significant differences.

If you are really concerned about performance, don't even use substring (check the code, it has 3 ifs and creates a new String every time you call it), but you should operate with char arrays.

And then again: in real life I don't think it will matter. Run the JMH test with some real parameters (lengths of strings and constant values). I think you'll see numbers which are enough for almost every sane use-case.

D. Kovács
  • 1,232
  • 13
  • 25
0

The branching may cost something like a few nanoseconds. In both cases there's no needless char[] copying. The method call overhead is rather big, but gets optimized out.

A few nanoseconds times 200 make a few microseconds at most. The difference between the two approaches is smaller, so you may spend 0.0001% the time on this. This is a very rough estimate, but even if I was off by a factor of thousand, there's no point in optimizing here.

maaartinus
  • 44,714
  • 32
  • 161
  • 320