[Mono-list] Compile Error (System.Diagnostics.Stopwatch)

Tim Riley riltim at gmail.com
Thu Oct 19 11:19:02 EDT 2006


Using: Mono 1.1.18, Windows XP

A buddy of mine wrote a small C# app for testing the speed of text reversing
loops to find which method is most efficient. It compiles fine using .NET
2.0 but when I try to compile it using Mono 1.1.18 I get the following two
errors.

reverse_test_mono.cs(99,16): error CS0246: The type or namespace name
'Stopwatch' could not be found. Are you missing a using directive or an
assembly reference?
reverse_test_mono.cs(99,44): error CS0246: The type or namespace name
'Stopwatch' could not be found. Are you missing a using directive or an
assembly reference?

When I use Lutz Roeder's .NET Reflector on mono's System.dll found in the
"lib\mono\2.0" directory I can drill down to "System.Diagnostics.Stopwatch"
so I assume it should be available if I reference "System" when trying to
compile.

I am issuing this command to compile:
mcs -reference:System reverse_test_mono.cs

and here is the code:
#####################Code#####################
using System;
using System.Text;
using System.Diagnostics;

namespace CADLABS
{
    interface IReversable { string Reverse( string text ); }

    class Class1 : IReversable
    {
        public string Reverse( string text )
        {
            if ( text == null || text.Length < 2 ) return text;
            char[] chars = text.ToCharArray();
            Array.Reverse( chars );
            return new string( chars );
        }

        public override string ToString() { return "Class1"; }
    }

    class Class2 : IReversable
    {
        public string Reverse( string text )
        {
            if ( text == null || text.Length < 2 ) return text;
            char[] chars = new char[ text.Length ];
            for ( int i = 0, j = chars.GetUpperBound( 0 ) ; -1 < j ; )
                chars[ i++ ] = text[ j-- ];
            return new string( chars );
        }

        public override string ToString() { return "Class2"; }
    }

    class Class3 : IReversable
    {
        public string Reverse( string text )
        {
            if ( text == null || text.Length < 2 ) return text;
            StringBuilder sb = new StringBuilder( text );
            for ( int i = 0, j = text.Length - 1 ; -1 < j ; )
                sb[ i++ ] = text[ j-- ];
            return sb.ToString();
        }

        public override string ToString() { return "Class3"; }
    }

    class Class4 : IReversable
    {
        //  Glenn's code

        public string Reverse( string s )
        {
            // mp added following line to even the field
            if ( s == null || s.Length < 2 ) return s;
            // Allocate buffer...
            char[] reversedString = new char[ s.Length ];
            // Calculate length ONLY ONCE, NOT each time thru 'for' loop...
            int limit = s.Length / 2;
            for ( int i = 0, j = s.Length - 1 ; i <= limit ; i++, j-- )
            {
                reversedString[ i ] = s[ j ];
                reversedString[ j ] = s[ i ];
            }
            return new string( reversedString );
        }

        public override string ToString() { return "Class4"; }
    }

    class Class5 : IReversable
    {
        //  probably a bad interprettation of Cornbread's code
        //  not using pointers but using the triple xor technique

        public string Reverse( string text )
        {
            if ( text == null || text.Length < 2 ) return text;
            char[] chars = text.ToCharArray();
            int j = chars.GetUpperBound(0);

            for ( int i = 0 ; i < j ; i++, j-- )
            {
                chars[ i ] ^= chars[ j ];
                chars[ j ] ^= chars[ i ];
                chars[ i ] ^= chars[ j ];
            }

            return new string( chars );
        }

        public override string ToString() { return "Class5"; }
    }

    class Testing
    {
        static Stopwatch _stopwatch  = new Stopwatch();

        public static void Test( IReversable x, string text, int iterations
)
        {
            string result;

            _stopwatch.Reset();
            _stopwatch.Start();

            for ( int i = 0 ; i < iterations ; i++ )
                result = x.Reverse( text );

            _stopwatch.Stop();

            Console.WriteLine(
                "{0}.Reverse took {1} milliseconds ({2} chars, {3}
iterations).",
                x.ToString(),
                _stopwatch.ElapsedMilliseconds,
                text.Length,
                iterations
            );
        }

        public static void Main()
        {

            IReversable[] irTests = new IReversable[ 5 ];

            irTests[ 0 ] = new Class1();
            irTests[ 1 ] = new Class2();
            irTests[ 2 ] = new Class3();
            irTests[ 3 ] = new Class4();
            irTests[ 4 ] = new Class5();

            //  test 'em ==========================================

            string text = "0123456789abcdef";
            int iterations = 1000000;

            foreach ( IReversable irTest in irTests )
                Test( irTest, text, iterations );

            Console.WriteLine();

            //  test 'em ==========================================

            for ( int i = 0 ; i < 6 ; i++ )
                text = text + text;

            iterations = iterations / 10;

            foreach ( IReversable irTest in irTests )
                Test( irTest, text, iterations );

            Console.WriteLine();

            //  test 'em ==========================================

            for ( int i = 0 ; i < 6 ; i++ )
                text = text + text;

            iterations = iterations / 100;

            foreach ( IReversable irTest in irTests )
                Test( irTest, text, iterations );

            Console.ReadKey();
        }
    }
}
#####################Code#####################
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.ximian.com/pipermail/mono-list/attachments/20061019/d545eec6/attachment-0001.html 


More information about the Mono-list mailing list