- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
考虑以下代码:
using System;
using System.Diagnostics;
namespace Demo
{
class Program
{
static void Main(string[] args)
{
Stopwatch sw = new Stopwatch();
for (int trial = 0; trial < 4; ++trial)
{
sw.Restart();
loop1();
Console.WriteLine("loop1() took " + sw.Elapsed);
sw.Restart();
loop2();
Console.WriteLine("loop2() took " + sw.Elapsed);
sw.Restart();
loop3();
Console.WriteLine("loop3() took " + sw.Elapsed);
// Console.WriteLine(); // <-- Uncomment this and the timings change a LOT!
}
}
static void loop1()
{
bool done = false;
for (int i = 0; i < 100000 && !done; ++i)
{
for (int j = 0; j < 100000 && !done; ++j)
{
for (int k = 0; k < 2; ++k)
{
if (i == 9900)
{
done = true;
break;
}
}
}
}
}
static void loop2()
{
for (int i = 0; i < 100000; ++i)
{
for (int j = 0; j < 100000; ++j)
{
for (int k = 0; k < 2; ++k)
{
if (i == 9900)
{
goto exit;
}
}
}
}
exit: return;
}
static void loop3()
{
for (int i = 0; i < 100000; ++i)
{
for (int j = 0; j < 100000; ++j)
{
for (int k = 0; k < 2; ++k)
{
if (i == 9900)
{
k = 2;
j = 100000;
i = 100000;
}
}
}
}
}
}
}
当我使用 Visual Studio 2010 在 Windows 7 x64 上编译并运行此版本的 RELEASE x86 版本时,我得到以下计时(在 Intel Core i7 上运行)
loop1() took 00:00:01.7935267
loop2() took 00:00:01.4747297
loop3() took 00:00:05.6677592
loop1() took 00:00:01.7654008
loop2() took 00:00:01.4818888
loop3() took 00:00:05.7656440
loop1() took 00:00:01.7990239
loop2() took 00:00:01.5019258
loop3() took 00:00:05.7979425
loop1() took 00:00:01.8356245
loop2() took 00:00:01.5688070
loop3() took 00:00:05.7238753
这本身就很奇怪——为什么 loop3() 会比其他循环慢那么多?无论如何,然后我取消注释指示的行(Console.WriteLine()),我的时间变为:
loop1() took 00:00:01.8229538
loop2() took 00:00:07.8174210
loop3() took 00:00:01.4879274
loop1() took 00:00:01.7691919
loop2() took 00:00:07.4781999
loop3() took 00:00:01.4810248
loop1() took 00:00:01.7749845
loop2() took 00:00:07.5304738
loop3() took 00:00:01.4634904
loop1() took 00:00:01.7521282
loop2() took 00:00:07.6325186
loop3() took 00:00:01.4663219
现在 loop2() 慢得多,而 loop3() 快得多。我觉得这很好奇......
所以我有两个问题:
[编辑] 我应该补充一点,我可以用秒表验证这些计时,并且我正在从命令行运行测试程序(因此我们可以排除 Visual Studio 对其进行干扰)。
附录:
我对程序进行了如下修改,以排除 JITTER 正在优化循环的可能性:
using System;
using System.Diagnostics;
using System.Text;
namespace Demo
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(test());
}
static string test()
{
Stopwatch sw = new Stopwatch();
int total = 0;
StringBuilder builder = new StringBuilder();
for (int trial = 0; trial < 2; ++trial)
{
sw.Restart();
total += loop1();
builder.AppendLine("loop1() took " + sw.Elapsed);
sw.Restart();
total += loop2();
builder.AppendLine("loop2() took " + sw.Elapsed);
sw.Restart();
total += loop3();
builder.AppendLine("loop3() took " + sw.Elapsed);
//builder.AppendLine(); // Uncommenting this line makes a big difference!
}
builder.AppendLine(total.ToString());
return builder.ToString();
}
static int loop1()
{
bool done = false;
int total = 0;
for (int i = 0; i < 100000 && !done; ++i)
{
for (int j = 0; j < 100000 && !done; ++j)
{
for (int k = 0; k < 2; ++k)
{
if (i == 9900)
{
done = true;
break;
}
++total;
}
}
}
return total;
}
static int loop2()
{
int total = 0;
for (int i = 0; i < 100000; ++i)
{
for (int j = 0; j < 100000; ++j)
{
for (int k = 0; k < 2; ++k)
{
if (i == 9900)
{
goto exit;
}
++total;
}
}
}
exit: return total;
}
static int loop3()
{
int total = 0;
for (int i = 0; i < 100000; ++i)
{
for (int j = 0; j < 100000; ++j)
{
for (int k = 0; k < 2; ++k)
{
if (i == 9900)
{
k = 2;
j = 100000;
i = 100000;
}
else
{
++total;
}
}
}
}
return total;
}
}
}
现在我的结果如下:
builder.AppendLine() 注释掉了:
loop1() took 00:00:06.6509471
loop2() took 00:00:06.7322771
loop3() took 00:00:01.5361389
loop1() took 00:00:06.5746730
loop2() took 00:00:06.7051531
loop3() took 00:00:01.5027345
-1004901888
builder.AppendLine() 未注释掉:
loop1() took 00:00:06.9444200
loop2() took 00:00:02.8960563
loop3() took 00:00:01.4759535
loop1() took 00:00:06.9036553
loop2() took 00:00:03.1514154
loop3() took 00:00:01.4764172
-1004901888
请注意我这样做时 loop2() 计时的差异。不计算!
最佳答案
我可以准确重现。此外,我还可以像这样消除方差:
private static void Main(string[] args)
{
Stopwatch sw = new Stopwatch();
for (int trial = 0; trial < 4; ++trial)
{
sw.Restart();
Interlocked.MemoryBarrier();
loop1();
Interlocked.MemoryBarrier();
Console.WriteLine("loop1() took " + sw.Elapsed);
sw.Restart();
Interlocked.MemoryBarrier();
loop2();
Interlocked.MemoryBarrier();
Console.WriteLine("loop2() took " + sw.Elapsed);
sw.Restart();
Interlocked.MemoryBarrier();
loop3();
Interlocked.MemoryBarrier();
Console.WriteLine("loop3() took " + sw.Elapsed);
// Console.WriteLine(); // <-- Uncomment this and the timings don't change now.
}
}
当我使用 MemoryBarriers 运行时,无论我以何种方式运行测试,我都会得到第二种模式:
loop1() took ~1 sec
loop2() took ~7 secs
loop3() took ~1 sec
MemoryBarrier 定义:
Synchronizes memory access as follows: The processor executing the current thread cannot reorder instructions in such a way that memory accesses prior to the call to MemoryBarrier execute after memory accesses that follow the call to MemoryBarrier.
由于循环的 IL 在两个版本之间是相同的,并且 MemoryBarrier 使方差消失,我认为我们可以得出结论,方差肯定是第一个模式优化的结果......也许是 JITer.. .也许是 CPU 的问题……不确定。
除了我使用的是 VS2012 和 .NET 4.5 RTM,我的环境是一样的。
关于c# - 在循环外添加一个 Console.WriteLine() 会改变循环的计时 - 为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11984176/
Console.WriteLine() 和有什么区别和Trace.WriteLine() ? 最佳答案 从“调试”的角度来看这些。 我们开始使用 Console.WriteLine() 进行调试 后来
Console.WriteLine() 与 Debug 之间有什么区别.WriteLine()? 最佳答案 Console.WriteLine在调试或发布中写入标准输出流。 Debug.WriteLi
由于这里的用户,我最近来到了 log4net。无论如何,我想将每个 Console.Write() 和 Debug.Write() 自动记录到我配置的单个日志文件中。此外,我使用了许多类库,这些类库也
webjob 的方法可以使用 TextWriter logger 作为参数。但如果我打电话: logger.WriteLine("Test"); Console.WriteLine("Test");
我有一些代码需要在运行测试时查看,并且TestContext 必须是我的测试类的一部分我需要显示正在测试的类的 debug.writelines。我考虑过只是将 TestContext 传递给我的 M
在我们的一个应用程序中,我遇到过这样的行: Console.WriteLine(string.Empty); 我检查了如果我只写它是否有任何不同: Console.WriteLine(); 但输出是相
考虑以下控制台应用程序代码: Thread.CurrentThread.CurrentCulture = new CultureInfo("en-GB"); Thread.CurrentThread.
我完全是 C# 的菜鸟和初级程序员,但我在阅读有关 Roslyn 和“C# 7.0 的新增功能”的文章时发现了一些非常有趣的东西,但我找不到我需要的答案。 在此link ,所有给出的示例都包含类似 W
哪个更快?哪个占用的内存更少? Console.WriteLine("string1") Console.WriteLine("string2") Console.WriteLine("string3
Afileobject f 具有以下 IO 方法,根据 Python in a Nutshell: f.read(size=-1) In v2, or in v3 when f is open in
为什么 Visual Studio 抛出错误 Console.WriteLine('string with single quote'); 而不是: Console.WriteLine("string
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
我依稀记得在 nUnit 中可能在 reSharper 或 TeamCity 的上下文中阅读了关于在 Console.Out.WriteLine 上使用 Trace.WriteLine 的“某事”“某
在我的 C# 代码中,我想将 Debug.WriteLine() 和 Console.WriteLine() 包装到一个函数中,以便它在调试时以调试窗口为目标模式和控制台处于 Release模式。实现
我正在制作一个 SchoolApp 程序来学习 C#,我有一个我正在努力实现的主要功能: namespace SchoolApp { class Program { p
我有一个日志文件 (.txt),其中包含以下信息: Filename1 - A3332NCDER Filename2 - B3332NCDER Filename3 - B1222NCDERE F
是否可以编写一个循环,将重叠文本写入控制台? 期望的输出: 注意线条是如何重叠的。我在用'|'垂直和'-'水平。 垂直线是第 4 列,水平线是第 3 行。 我知道这离我们很远: for (int i
这是我的代码: static void Main(string[] args) { if(args.Length > 1) { int id; if(i
为什么我们不能在 C# 中的 if 条件中编写 Console.Writeline()?但是我们可以写printf() if 条件 C 中的语句? 最佳答案 在 C 中,printf 的返回类型是 i
我对 VBA 相当陌生,并且对如何解决执行此代码时收到的“运行时错误'5':无效的过程调用或参数”错误感到困惑。有问题的单元格有中文字符,代码似乎在英文字母上运行良好。流正在输出到文本文件。 (将来应
我是一名优秀的程序员,十分优秀!