コード例 #1
0
        //-------------------------------------------------------------------------
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test(enabled = false) public void test_performance() throws Exception
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
        public virtual void test_performance()
        {
            ThreadLocal <DecimalFormat> thread = ThreadLocal.withInitial(() => new DecimalFormat("#,##0.###", new DecimalFormatSymbols(Locale.ENGLISH)));
            DecimalFormat   java   = new DecimalFormat("#,##0.###", new DecimalFormatSymbols(Locale.ENGLISH));
            NumberFormatter strata = NumberFormatter.of(true, 0, 3);
            Random          random = new Random(1);

            for (int i = 0; i < 20; i++)
            {
                long start0 = System.nanoTime();
                for (int j = 0; j < 100_000; j++)
                {
                    double val = random.NextDouble();
                    string str = java.format(val);
                    if (str.Length == 0)
                    {
                        throw new System.InvalidOperationException("Just to avoid dead code elimination: " + str);
                    }
                }
                long end0 = System.nanoTime();
                Console.WriteLine("  Java: " + ((end0 - start0) / 1_000_000d) + "ms");

                long start1 = System.nanoTime();
                for (int j = 0; j < 100_000; j++)
                {
                    double val = random.NextDouble();
                    string str = thread.get().format(val);
                    if (str.Length == 0)
                    {
                        throw new System.InvalidOperationException("Just to avoid dead code elimination: " + str);
                    }
                }
                long end1 = System.nanoTime();
                Console.WriteLine("JavaTL: " + ((end1 - start1) / 1_000_000d) + "ms");

                long start1b = System.nanoTime();
                for (int j = 0; j < 100_000; j++)
                {
                    double val = random.NextDouble();
                    string str = ((NumberFormat)java.clone()).format(val);
                    if (str.Length == 0)
                    {
                        throw new System.InvalidOperationException("Just to avoid dead code elimination: " + str);
                    }
                }
                long end1b = System.nanoTime();
                Console.WriteLine("JavaCl: " + ((end1b - start1b) / 1_000_000d) + "ms");

                long start2 = System.nanoTime();
                for (int j = 0; j < 100_000; j++)
                {
                    double val = random.NextDouble();
                    string str = strata.format(val);
                    if (str.Length == 0)
                    {
                        throw new System.InvalidOperationException("Just to avoid dead code elimination: " + str);
                    }
                }
                long end2 = System.nanoTime();
                Console.WriteLine("Strata: " + ((end2 - start2) / 1_000_000d) + "ms");
            }
        }