JulesKouatchou / basic_language_comparison

Other
85 stars 21 forks source link

Java-Implementation for `testLookAndSay` is inefficient #16

Open oli-h opened 2 years ago

oli-h commented 2 years ago

Please improve two Java-Codelines to avoid performance penaltys:

1.

result.append(times + "" + repeat);  // YOUR code creates (finally millions) intermediate StringBuilder's
result.append(times).append(repeat);  // Better: THIS code avoids it - you already have a StringBuuilder

2.

StringBuilder result = new StringBuilder(); // YOUR code creates Builder with a small (default-sized) buffer which must be increased over and over again while appending characters
StringBuilder result = new StringBuilder(number.length() * 2); // Better: THIS code directly creates a buffer with expected size

Please note that you also implemented in this optimized way for the C-version: https://github.com/JulesKouatchou/basic_language_comparison/blob/master/C/test_look_and_say.c#L18

JulesKouatchou commented 1 year ago

Thank you for your suggestion.