NanoPlugins / NanoMines

0 stars 0 forks source link

Change while to fatest MoneyFormat #1

Closed Kewilleen closed 4 years ago

Kewilleen commented 4 years ago

I saw you use while to get an index and convert to the format, this method is slow if calc 1k + requests.

https://github.com/NanoPlugins/NanoMines/blob/5cc87295633c7619e1556d954b89734790e38eea/src/main/java/com/nanoplugins/mines/util/NumberFormat.java#L17

Recently i create a best form to performance temporary and get great result, tested with: https://www.onlinegdb.com/online_java_compiler results ->

image

I'm not saying that your method is wrong or something, but it's a way I found out and wanted to help :)

    public static final double LOG = 6.907755278982137D;
    public static final Object[][] VALUES = {
      {"", "K", "M", "B", "T", "Q", "QQ", "S", "SS", "O", "N", "D", "UN", "DD", "TR", "QT", "QN", "SD", "SPD", "OD", "ND", "VG", "UVG", "DVG", "TVG", "QTV"},
      {1D, 1000.0D, 1000000.0D, 1.0E9D, 1.0E12D, 1.0E15D, 1.0E18D, 1.0E21D, 1.0E24D, 1.0E27D, 1.0E30D, 1.0E33D, 1.0E36D, 1.0E39D, 1.0E42D, 1.0E45D, 1.0E48D, 1.0E51D, 1.0E54D, 1.0E57D, 1.0E60D, 1.0E63D, 1.0E66D, 1.0E69D, 1.0E72D}
    };
    public static final DecimalFormat FORMAT = new DecimalFormat("#,###.##", new DecimalFormatSymbols(new Locale("pt", "BR")));

    public static String format(double number) {
        if (number == 0) return FORMAT.format(number);
        int index = (int) (Math.log(number) / LOG);
        return FORMAT.format(number / (double) VALUES[1][index]) + VALUES[0][index];
    }
M0rais commented 4 years ago

Thanks for the information, next time try to do a pull request, I'll be accepting some of them. About the code, I'll update as soon as I can.