-1

Im trying to see a performance difference between the following two programs (was expecting). But I find no difference. Is this normal? Im running on a Windows Core 2 Duo M/C Visual Studio 2010 Express Edition

Program 1 (averaged over 100 runs: 824.11 ms):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Threading;


namespace MultiThreading
{
    class Program
    {
        public static Stopwatch stopwatch;
        static void Main(string[] args)
        {
            stopwatch = new Stopwatch();
            stopwatch.Start();
            //Thread t = new Thread(WriteY);
            //t.Start();
            for (int i = 0; i < 10000; i++)
            {
                Console.Write("x{0} ", i);
            }

            WriteY();

            Console.WriteLine("Time taken in milliseconds: {0}", stopwatch.ElapsedMilliseconds);
            Console.ReadLine();
        }

        static void WriteY()
        {
            for (int i = 0; i < 10000; i++)
            {
                Console.Write("y{0} ", i);
            }
            //Console.WriteLine("Time taken in milliseconds: {0}", stopwatch.ElapsedMilliseconds);
            //Console.ReadLine();
        }

Program 2(averaged over 100 runs: 828.11 ms):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Threading;


namespace MultiThreading
{
    class Program
    {
        public static Stopwatch stopwatch;
        static void Main(string[] args)
        {
            stopwatch = new Stopwatch();
            stopwatch.Start();
            Thread t = new Thread(WriteY);
            t.Start();
            for (int i = 0; i < 10000; i++)
            {
                Console.Write("x{0} ", i);
            }

            //WriteY();

            Console.WriteLine("Time taken in milliseconds: {0}", stopwatch.ElapsedMilliseconds);
            Console.ReadLine();
        }

        static void WriteY()
        {
            for (int i = 0; i < 10000; i++)
            {
                Console.Write("y{0} ", i);
            }
            //Console.WriteLine("Time taken in milliseconds: {0}", stopwatch.ElapsedMilliseconds);
            //Console.ReadLine();
        }
    }
}
arhsim
  • 135
  • 2
  • 8
  • 2
    -1. You dont have ANY computation in thee, so "computattionally intensive" is a basic misunderstanding waht this means. Isntead all you do is Console.WriteLine IO which is serialized by the console output mechanism. – TomTom Jan 06 '12 at 14:58

2 Answers2

8

My guess is that both are bound by the speed of Console.Write which presumably needs to lock the resource (the screen) so that only one thread accesses it at a time.

Jeff Foster
  • 43,770
  • 11
  • 86
  • 103
4

The problem is that your application is IO-bound because you use Console.WriteLine all the time. If you do something which isn't using IO you will see a boost.

And as mentioned in the other answer WriteLine does indeed synchronize: Calling Console.WriteLine from multiple threads

Community
  • 1
  • 1
Lasse Espeholt
  • 17,622
  • 5
  • 63
  • 99
  • Without the `Console.WriteLine` the program will almost certainly finish (near) instantaneously, with more overhead spent on spinning threads in the second example. I'd expect the 1st one to finish first. Of course, with no loop body, the compiler will probably omit the loops altogether (in release mode), making it a measure of spinning up a thread vs not spinning up a thread. – spender Jan 06 '12 at 15:01
  • @spender Yeah, it all depends on what `WriteLine` is replaced by. If it is replaced by nothing it is, as you say, probably replaced by nothing. But if it's replaced by a large matrix multiplcation then they might be a near 50% cut in running time (depending on cache etc.) – Lasse Espeholt Jan 06 '12 at 15:05