C#程序在多个实例上运行缓慢 [英] C# program runs slowly on multiple instances
本文介绍了C#程序在多个实例上运行缓慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
现在我正在练习一些C#程序。我在这个程序中遇到了一个问题。
我创建了这个程序,执行时间为21秒,CPU使用率为20%,Ram使用率为1Gb max。
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();
}
但是当我创建2个这样的实例时耗时140秒,CPU使用率为20%且Ram使用率为最多1GB。
你能帮助我吗,我怎样才能运行多个实例,需要21秒才能最大限度地利用我的Ram和CPU。
我尝试过:
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.
What I have tried:
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();
}
推荐答案
我主要按照你的方式尝试了它,花了39秒。当然,我不知道你的秒表课程的内容,但是当我打电话给我的 ElapsedTime $ c时,我所做的就是返回 TimeSpan
$ c>方法。我也只使用 string.Concat
结束值只调用 Console.Writeline
。
I tried it mostly your way and it took 39 seconds. Granted, I don't know the contents of your Stopwatch class, but all mine does is return aTimeSpan
when I call myElapsedTime
method. I'm also making only one call toConsole.Writeline
with astring.Concat
endated value.
public class StopWatch
{
public DateTime StartTime { get; set; }
public void Start()
{
this.StartTime = DateTime.Now;
}
public TimeSpan ElapsedTime()
{
return (DateTime.Now - this.StartTime);
}
}
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(string.Concat("Acccessed Value: ", i.ToString(), ", Time: ", sw.ElapsedTime().ToString()));
}
}
Console.Read();
}
出于好奇,我尝试了运行单个应用程序的多线程方法。这也是47秒。
Out of curiosity, I tried a multi-threaded approach running a single app. This too 47 seconds.
static void Main(string[] args)
{
string str = Console.ReadLine();
if (str == "start")
{
StopWatch sw = new StopWatch();
sw.Start();
Parallel.Invoke(()=>
{
for (int i = 1; i < 200000; i++)
{
Console.WriteLine(string.Concat("Thread 1, Acccessed Value: ", i.ToString(), ", Time: ", sw.ElapsedTime().ToString()));
}
},
()=>
{
for (int i = 1; i < 200000; i++)
{
Console.WriteLine(string.Concat("Thread 2, Acccessed Value: ", i.ToString(), ", Time: ", sw.ElapsedTime().ToString()));
}
});
}
Console.Read();
}
最后,我想知道共享相同的秒表是否会导致某种自我延迟,所以我做了这个,导致大约49秒的时间流逝。
And finally, I wondered if sharing the same stopwatch might be causing some sort of self-induced delays, so I did this, which resulted in about 49 seconds of elapsed timed.
static void Main(string[] args)
{
string str = Console.ReadLine();
if (str == "start")
{
StopWatch sw = new StopWatch();
sw.Start();
Parallel.Invoke(()=>RunTask(1), ()=>RunTask(2));
}
Console.Read();
}
private static void RunTask(int taskno)
{
StopWatch sw = new StopWatch();
sw.Start();
string taskNumber = taskno.ToString();
for (int i = 1; i < 200000; i++)
{
Console.WriteLine(string.Concat("Thread ", taskNumber," , Acccessed Value: ", i.ToString(), ", Time: ", sw.ElapsedTime().ToString()));
}
}
这篇关于C#程序在多个实例上运行缓慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文