请注意此问题仅与性能有关.让我们跳过设计指南,理念,兼容性,可移植性以及与纯性能无关的任何内容.谢谢.
现在回答这个问题.我一直认为,因为C#getters/setter实际上是伪装的方法,所以读取公共字段必须比调用getter更快.
所以要确保我做了一个测试(下面的代码).但是,如果从Visual Studio内部运行,此测试仅产生预期结果(即字段比34%的getter快).
一旦从命令行运行它,它显示几乎相同的时间......
唯一的解释可能是CLR做了额外的优化(如果我错了,请纠正我).
我不相信在实际应用中,以更复杂的方式使用这些属性,它们将以相同的方式进行优化.
请帮助我证明或反驳现实生活中的属性比田地慢的想法.
问题是 - 我应该如何修改测试类以使CLR改变行为,以便公共字段超越getter.或者告诉我,任何没有内部逻辑的属性都会像字段一样执行(至少在getter上)
编辑:我只谈论发布x64版本.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace PropertyVsField
{
class Program
{
static int LEN = 20000000;
static void Main(string[] args)
{
List<A> a = new List<A>(LEN);
List<B> b = new List<B>(LEN);
Random r = new Random(DateTime.Now.Millisecond);
for (int i = 0; i < LEN; i++)
{
double p = r.NextDouble();
a.Add(new A() { P = p }); …
Run Code Online (Sandbox Code Playgroud) 我发现了很多示例如何将控制台输出重定向到文件中.但是我需要一个相反的解决方案 - 我有StreamWriter,我希望在控制台输出中显示它sw.WriteLine("text");
我有一个超过1000个时间戳(下面)的pandas数据帧,我想循环遍历:
2016-02-22 14:59:44.561776
Run Code Online (Sandbox Code Playgroud)
我很难将这个时间戳分成两列 - '日期'和'时间'.日期格式可以保持不变,但需要将时间转换为CST(包括毫秒).
谢谢您的帮助
我刚刚建立了动态方法 - 见下文(感谢SO用户).似乎Func创建为动态方法,IL注入比lambda慢2倍.
谁知道为什么呢?
(编辑:这是在VS2010中作为Release x64构建的.请从控制台运行,而不是从Visual Studio F5内部运行.)
class Program
{
static void Main(string[] args)
{
var mul1 = IL_EmbedConst(5);
var res = mul1(4);
Console.WriteLine(res);
var mul2 = EmbedConstFunc(5);
res = mul2(4);
Console.WriteLine(res);
double d, acc = 0;
Stopwatch sw = new Stopwatch();
for (int k = 0; k < 10; k++)
{
long time1;
sw.Restart();
for (int i = 0; i < 10000000; i++)
{
d = mul2(i);
acc += d;
}
sw.Stop();
time1 = sw.ElapsedMilliseconds;
sw.Restart();
for (int …
Run Code Online (Sandbox Code Playgroud) 我完全不解.如果在我从未测试过的线程中存在未捕获的异常,我非常确定.NET会关闭整个应用程序域.
但是我只是尝试了下面的代码并且它没有失败......任何人都可以解释为什么?
(在.NET 4和3.5中尝试过)
static void Main(string[] args)
{
Console.WriteLine("Main thread {0}", Thread.CurrentThread.ManagedThreadId);
Action a = new Action(() =>
{
Console.WriteLine("Background thread {0}", Thread.CurrentThread.ManagedThreadId);
throw new ApplicationException("test exception");
});
a.BeginInvoke(null, null);
Console.ReadLine();
}
Run Code Online (Sandbox Code Playgroud) 我可以使用C#app中的DirectX 11吗?
需要从WPF应用程序使用DirectSound..NET 4 W7 x64
我看到很多关于"视觉工作室混合"的内容,我理解W8版本的混合.我在W8上安装了VS2012 Ultimate,我看不到任何混合.
我查看了我的MSDN Pro订阅,我没有看到任何单独的Blend for VS2012下载而不是VS2012 with Blend下载..
我怎么得到它?
在WPF工具包datagrid中,我有一个数据触发器绑定到单元格元素的不透明度.
当UpVisibility
路径变为1时,动画开始淡化为0.哪个有效.
但是我的问题现在 - 如果我需要提前停止/取消淡入淡出并且设置UpVisibility
为0,路径仍然可见并且随着任何事情的发生而消失....
如何使用MyValue对象立即将不透明度降为0?
<Path Data="M 5,0 0,10 10,10" Height="10" Width="10" Fill="Green" Opacity="{Binding MyValue[0].UpVisibility}" Margin="5,0,5,0">
<Path.Style>
<Style>
<Style.Triggers>
<DataTrigger Binding="{Binding MyValue[0].UpVisibility}" Value="1.0">
<DataTrigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetProperty="Opacity" From="1.0" To="0.0" Duration="0:0:10" />
</Storyboard>
</BeginStoryboard>
</DataTrigger.EnterActions>
</DataTrigger>
</Style.Triggers>
</Style>
</Path.Style>
</Path>
Run Code Online (Sandbox Code Playgroud) 我有一个KeyValuePairs列表.我通常会用ToDictionary
.
但是我只是注意到错误消息(如下所示)有关于显式强制转换的内容,这意味着我实际上可以将列表转换为Dictionary<...>
.我怎样才能做到这一点?
Cannot implicitly convert type 'System.Linq.IOrderedEnumerable<System.Collections.Generic.KeyValuePair<int,string>>' to 'System.Collections.Generic.Dictionary<int, string>'. An explicit conversion exists (are you missing a cast?)
示例代码:
Dictionary<int, string> d = new Dictionary<int, string>() {
{3, "C"},
{2, "B"},
{1, "A"},
};
var s = d.OrderBy(i => i.Value);
d = s;
Run Code Online (Sandbox Code Playgroud) c# ×7
.net ×3
c#-4.0 ×2
.net-4.0 ×1
.net-4.5 ×1
benchmarking ×1
binding ×1
blend ×1
blend-2012 ×1
datagrid ×1
delegates ×1
directx ×1
directx-11 ×1
gpu ×1
multimedia ×1
nvidia ×1
opencl ×1
optimization ×1
pandas ×1
performance ×1
python ×1
windows-8 ×1
wpf ×1
wpfdatagrid ×1
xaml ×1
xeon-phi ×1