下面的代码是检查执行相同解决方案的三种不同方式的性能.
public static void Main(string[] args)
{
// for loop
{
Stopwatch sw = Stopwatch.StartNew();
int accumulator = 0;
for (int i = 1; i <= 100000000; ++i)
{
accumulator += i;
}
sw.Stop();
Console.WriteLine("time = {0}; result = {1}", sw.ElapsedMilliseconds, accumulator);
}
//Enumerable.Range
{
Stopwatch sw = Stopwatch.StartNew();
var ret = Enumerable.Range(1, 100000000).Aggregate(0, (accumulator, n) => accumulator + n);
sw.Stop();
Console.WriteLine("time = {0}; result = {1}", sw.ElapsedMilliseconds, ret);
}
//self-made IEnumerable<int>
{
Stopwatch sw = Stopwatch.StartNew();
var ret = …
Run Code Online (Sandbox Code Playgroud) 我打算用C++编写一个游戏,它将是CPU密集型的(寻路,遗传算法,神经网络......)所以我一直在考虑如何最好地解决这种情况,以便它能运行顺利.
(让这个问题的上半部分是附带信息,我不希望它限制主要问题,但如果你能给我旁注,那将会很好)
是否值得学习如何使用ASM,因此我可以用C++进行ASM调用,它能给我一个显着/显着的性能优势吗?
我应该在什么情况下使用它?
我目前正试图解决问题"从1到20的所有数字可以被整除的最小正数是多少?"
到目前为止,我编写了一些似乎有用的内容,但需要很长时间.此外,我需要在if中使用大量的'和'语句,这看起来效率不高也不专业.
我该怎么做才能优化这些代码并使其更整洁?
number = 1
result = 0
def divide(candidate):
if candidate % 2 == 0 and candidate % 3 == 0 and candidate % 4 == 0 and candidate % 5 == 0 and candidate % 6 == 0 and candidate % 7 == 0 and candidate % 8 == 0 and candidate % 9 == 0 and candidate % 10 == 0 and candidate % 11 == 0 and candidate % 12 == 0 and candidate % …
Run Code Online (Sandbox Code Playgroud) performance ×2
assembly ×1
c# ×1
c++ ×1
divide ×1
enumerable ×1
ienumerable ×1
math ×1
python ×1
range ×1