0

Now a days I'm practicing some programs of C#. I have an issue in this program.

I have created this program and it took 21 seconds to execute and my CPU usage is 20% and Ram usage is 1Gb max.

      static void Main(string[] args)
    {
        string str = Console.ReadLine();

        if (str == "start")
        {
            Stopwatch sw = new Stopwatch();
            sw.Start();
            for (int i = 1; i < 200000; i++)
            {


                Console.WriteLine("Acccessed Value" + i.ToString());
                Console.WriteLine("Time " + sw.ElapsedMilliseconds);

            }
        }
        Console.Read();
    }

but when I create 2 instances of this It took 140 seconds and CPU usage is 20 % and Ram usage is 1GB max.

Can you please help me, how can I run multiple instances which will take 21 seconds but can utilize my Ram and CPU as maximum.

2 Answers2

1

You don't want to start different instances. Try using Tasks in your application, to utilize multiple cores of your CPU. Create Environment.ProcessorCount number of tasks and run the operations on them. There is a higher-level of abstraction too - Parallel, which you can look into.

hyankov
  • 4,049
  • 1
  • 29
  • 46
  • Except the main work of this program appears to be just outputting to the console. Running extra copies within the process will just slow things down due to locking around console access. We have no idea what the real task is that the user is working on and hence no idea whether it's naturally resource constrained (as the console is) or if additional tasks would help. – Damien_The_Unbeliever Jan 11 '17 at 07:22
  • I tried this with this code, If I execute one task it takes 21 seconds and on two tasks it takes 43 seconds each. Here is the code:Task _task = new Task(()=> GetTest()); Task _tasknew = new Task(()=> GetTest1()); _task.Start(); _tasknew.Start(); – Coding_hell Jan 11 '17 at 07:30
0

You are using Console.WriteLine method which is an IO method, and does not scale well for multi-threaded operations (see here), and does not support asynchronous operations. So you are likely to not to have a control over this.

But the question is, do you really need such an application? I don't think so; no body wants to write that amount of text for output at once. Writing into file, maybe, which supports asynchronous operations.

As a simple improvement, you can use StringBuilder instead of creating many short-lived String objects as follows:

 static void Main(string[] args)
{
    string str = Console.ReadLine();

    if (str == "start")
    {
        Stopwatch sw = new Stopwatch();

        var builder = new StringBuilder();

        for (int i = 1; i < 200000; i++)
        {
            sw.Start();

            builder.Clear();
            string a = builder.Append("Acccessed Value").Append(i.ToString()).ToString();

            builder.Clear();
            string b = builder.Append("Time ").Append(sw.ElapsedMilliseconds);

            Console.WriteLine(a);
            Console.WriteLine(b);

        }
    }
    Console.Read();
}
Community
  • 1
  • 1
Ghasan غسان
  • 5,577
  • 4
  • 33
  • 44
  • @Coding_hell, try without `Console.WriteLine`, and see. IO operations, specially outputting to terminal, takes time, and you cannot escape from this even with multi-threaded code. It is like having a Ferrari driving in a back alley; it can never perform as it would in a real track. – Ghasan غسان Jan 11 '17 at 11:51