Esempio n. 1
0
        public void String8_ToIntegerPerformance()
        {
            string  one  = "123456789";
            String8 one8 = one.TestConvert();

            long value = 0;
            long sum   = 0;

            int       iterations = 1 * 1000 * 1000;
            Stopwatch w          = Stopwatch.StartNew();

            for (int i = 0; i < iterations; ++i)
            {
                one8.TryToLong(out value);
                sum += value;
            }
            w.Stop();

            sum = 0;
            Stopwatch w2 = Stopwatch.StartNew();

            for (int i = 0; i < iterations; ++i)
            {
                long.TryParse(one, out value);
                sum += value;
            }
            w2.Stop();

            // Validate TryToLong is at least 2x as fast as long.TryParse
            Assert.IsTrue(w.ElapsedMilliseconds * 2 < w2.ElapsedMilliseconds);
            Trace.WriteLine($"{w.ElapsedMilliseconds:n0}ms String8.TryToLong; {w2.ElapsedMilliseconds:n0}ms long.TryParse.");
        }
Esempio n. 2
0
        private static void TryNumberConversions(string value)
        {
            String8 value8 = value.TestConvert();

            // .NET Parses "-0" successfully as int and long, but not ulong.
            // I don't want "-0" to be considered valid on any parse.
            if (value == "-0")
            {
                value = "Invalid";
            }

            int expectedInt, actualInt;

            Assert.AreEqual(int.TryParse(value, out expectedInt), value8.TryToInteger(out actualInt));
            Assert.AreEqual(expectedInt, actualInt);

            long expectedLong, actualLong;

            Assert.AreEqual(long.TryParse(value, out expectedLong), value8.TryToLong(out actualLong));
            Assert.AreEqual(expectedLong, actualLong);

            ulong expectedULong, actualULong;

            Assert.AreEqual(ulong.TryParse(value, out expectedULong), value8.TryToULong(out actualULong));
            Assert.AreEqual(expectedULong, actualULong);
        }