Java. How can I improve performance?
Java. How can I improve performance?
I have exercise below:
The Lavin Interactive Company, which has developed the turn-based strategy Losers-V, is constantly extending its target market by localizing the game to as many languages as it can. In particular, they are interested in creating a version of the game in Anindilyakwa, which is one of the languages spoken by indigenous Australians. However, the localization is complicated by the fact that Anindilyakwa has no numerals. How can a phrase such as ?You have seven black dragons and your enemy has forty black dragons? be translated into this language? The localizers have decided to translate it as follows: ?You have few black dragons and your enemy has lots of black dragons.? They have compiled a table showing the rule of replacing numbers of monsters by Anindilyakwa words.
And my implementation below:
import java.util.Scanner; public class Localization { public static void main(String[] args) { Scanner s = new Scanner(System.in); int number; String designation; number = s.nextInt(); if (number >= 1 && number <= 4) { designation = "few"; }else if(number >= 5 && number <= 9){ designation = "several"; }else if(number >= 10 && number <= 19){ designation = "pack"; }else if(number >= 20 && number <= 49) { designation = "lots"; }else if(number >= 50 && number <= 99){ designation = "horde"; }else if(number >= 100 && number <= 249){ designation = "throng"; }else if(number >= 250 && number <= 499){ designation = "swarm"; }else if(number >= 500 && number <= 999){ designation = "zounds"; }else{ designation = "legion"; } System.out.println(designation); } }
I loaded my code on competition server. And I see next statistics:
Execution time: 0.109
Memory used: 1 434 KB
After this I checked top level results and what I saw:
Rank 1:
Execution time: 0.062
Memory used: 78 KB
Conclusion:
my code two times slower; my code is used 20 times more memory.
My question: How? How? How is it possible? Why is my code so stupid? What do I need change to improve my code??
Answer by MByD for Java. How can I improve performance?
You can cut the checks in the if-else statements like this. Instead of:
if (number >= 1 && number <= 4) { designation = "few"; }else if(number >= 5 && number <= 9){ // ....
You can check only for upper bounds:
if (number >= 1 && number < 5) { designation = "few"; }else if(number < 10){ // ...
Answer by bmargulies for Java. How can I improve performance?
You could reduce memory by avoiding the Scanner. Just read a line via an InputStreamReader and parse it with the Integer class.
Answer by thedan for Java. How can I improve performance?
Assuming you are using the BufferedStream input idea from another answer, instead of using try-catch blocks to safely convert the input to an integer you can throw Exception from the main method. This prevents you from loading any specialized Exceptions.
I came up with .078 sec with 366 kb of memory. The best one I saw was .062 with 18kb of memory. That is just insane.
Answer by fiso for Java. How can I improve performance?
If you want to improve the execution time you should do the System.out.println inside of the condition followed with a return. Also you won't need to create the designation var.
}else if(number >= 5 && number <= 9){ System.out.println("several"); return;
Answer by Stefan Haustein for Java. How can I improve performance?
Not sure about the memory issue. The time and memory is probably dominated by library code (println, scanner). I think println flushes the stream for instance, which is expensive.
To reduce the average and maximum number of comparison, you could try to take advantage of how the numbers are distributed. Without knowing that, you could use binary search (here done over the buckets, you could also do it over the number space which would produce a different tree):
if (number < 50) { if (number < 10) { if (number < 5) { designation = "few"; } else { designation = "several"; } } else { if(number < 20) { designation = "pack"; } else { designation = "lots"; } } else { if (number < 250) { if (number < 100) { designation = "horde"; } else { designation = "throng"; } } else { if (number number < 500) { designation = "swarm"; } else if (number number < 1000) { designation = "zounds"; } else{ designation = "legion"; } } }
This code needs 4 comparisons to get to 1000 instead of 8.
Answer by durron597 for Java. How can I improve performance?
I got it down to 370 KB, 0.78 sec with this code. Kinda bored to get it further...
import java.io.IOException; public class Localization { public static void main (String[] args) { short s = 0; while (true) { int next = 0; try { next = System.in.read(); } catch (IOException e) { } if (next < '0' || next > '9') break; s = (short) (s * 10); s = (short) (s + ((short) (next - '0'))); } if (s >= 100) { if (s >= 500) { if (s >= 1000) { System.out.print('l'); System.out.print('e'); System.out.print('g'); System.out.print('i'); System.out.print('o'); System.out.print('n'); } else { System.out.print('z'); System.out.print('o'); System.out.print('u'); System.out.print('n'); System.out.print('d'); System.out.print('s'); } } else { if (s >= 250) { System.out.print('s'); System.out.print('w'); System.out.print('a'); System.out.print('r'); System.out.print('m'); } else { System.out.print('t'); System.out.print('h'); System.out.print('r'); System.out.print('o'); System.out.print('n'); System.out.print('g'); } } } else { if (s >= 10) { if (s >= 50) { System.out.print('h'); System.out.print('o'); System.out.print('r'); System.out.print('d'); System.out.print('e'); } else if (s >= 20) { System.out.print('l'); System.out.print('o'); System.out.print('t'); System.out.print('s'); } else { System.out.print('p'); System.out.print('a'); System.out.print('c'); System.out.print('k'); } } else { if (s >= 5) { System.out.print('s'); System.out.print('e'); System.out.print('v'); System.out.print('e'); System.out.print('r'); System.out.print('a'); System.out.print('l'); } else { System.out.print('f'); System.out.print('e'); System.out.print('w'); } } } System.out.println(); System.out.flush(); } }
Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72
0 comments:
Post a Comment