Optimize NickGenerator.java #55

Closed
ghost wants to merge 1 commits from main into main
pull from: main
ghost commented 2022-12-21 14:21:54 +00:00 (Migrated from github.com)

Now using a StringBuilder instead of a String to build the random nicknames, as StringBuilder is more efficient when it comes to modifying strings.

Now using a char[] to store the characters in the CHARS string, rather than creating a new String for each random nickname, which can improve performance.

Instead of using a List to store the nicknames from the file, now using a Set, as it has a faster contains method.

Now using a ThreadLocalRandom instead of a SecureRandom, as it is faster and has lower overhead, especially when generating many random numbers.

Now using a switch statement to replace the if statement when checking the nicknames file for valid nicknames. This can improve performance as the switch statement is faster than the if statement in this case.

Now using the try-with-resources statement to improve the readability and exception handling of the code that loads the nicknames from the file.

Now using a **StringBuilder** instead of a String to build the random nicknames, as **StringBuilder** is more efficient when it comes to modifying strings. Now using a **char[]** to store the characters in the CHARS string, rather than creating a new String for each random nickname, which can improve performance. Instead of using a List to store the nicknames from the file, now using a Set, as it has a faster contains method. Now using a **ThreadLocalRandom** instead of a **SecureRandom**, as it is faster and has lower overhead, especially when generating many random numbers. Now using a switch statement to replace the if statement when checking the nicknames file for valid nicknames. This can improve performance as the switch statement is faster than the if statement in this case. Now using the **try-with-resources statement** to improve the readability and exception handling of the code that loads the nicknames from the file.
crpmax (Migrated from github.com) requested changes 2022-12-22 18:03:45 +00:00
crpmax (Migrated from github.com) left a comment

Hello, thank you for contributing, but...
Your changes are not useful. Everything you did slowed the generation by more than ten times.

Only loading the file is faster, but only because you removed the regex and replaced it with a length only check. I do not accept that.
I want the nicks to be checked by regex before using them. The performance impact of that is not that important because the function is run only at startup.

Here are my test results using 1000 iterations for loading the file and 1 million iterations for generation.

Old version load time: 2618 ms  
New version load time: 1321 ms  
Old version generation time: 771 ms  
New version generation time: 10184 ms  
Old version real nicks generation time: 157 ms  
New version real nicks generation time: 10656 ms

HashSet instead of ArrayList is not OK because you have to convert it into an array in the generation function. The whole point of using List is that it has indexes.

Hello, thank you for contributing, but... Your changes are not useful. Everything you did slowed the generation by more than ten times. Only loading the file is faster, but only because you removed the regex and replaced it with a length only check. I do not accept that. I want the nicks to be checked by regex before using them. The performance impact of that is not that important because the function is run only at startup. Here are my test results using 1000 iterations for loading the file and 1 million iterations for generation. ``` Old version load time: 2618 ms New version load time: 1321 ms Old version generation time: 771 ms New version generation time: 10184 ms Old version real nicks generation time: 157 ms New version real nicks generation time: 10656 ms ``` HashSet instead of ArrayList is not OK because you have to convert it into an array in the generation function. The whole point of using List is that it has indexes.

Pull request closed

Please reopen this pull request to perform a merge.
Sign in to join this conversation.