- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我注意到对已转换为通用接口(interface)集合(IList 或 IEnumberable)的原始集合 (T[]) 进行迭代会影响性能。
例如:
private static int Sum(int[] array)
{
int sum = 0;
foreach (int i in array)
sum += i;
return sum;
}
上面的代码执行速度明显快于下面的代码,其中参数更改为类型 IList(或 IEnumerable):
private static int Sum(IList<int> array)
{
int sum = 0;
foreach (int i in array)
sum += i;
return sum;
}
如果传递的对象是原始数组,并且我尝试将循环更改为 for 循环而不是 foreach 循环,性能仍然会受到影响。
我可以通过这样编码来解决性能问题:
private static int Sum(IList<int> array)
{
int sum = 0;
if( array is int[] )
foreach (int i in (int[])array)
sum += i;
else
foreach (int i in array)
sum += i;
return sum;
}
有没有更优雅的方法来解决这个问题?感谢您的宝贵时间。
编辑:我的基准代码:
static void Main(string[] args)
{
int[] values = Enumerable.Range(0, 10000000).ToArray<int>();
Stopwatch sw = new Stopwatch();
sw.Start();
Sum(values);
//Sum((IList<int>)values);
sw.Stop();
Console.WriteLine("Elasped: {0} ms", sw.ElapsedMilliseconds);
Console.Read();
}
最佳答案
最好的办法是为 Sum
创建过载与 int[]
如果此方法对性能至关重要,则作为参数。 CLR 的 JIT 可以检测数组上的 foreach 式迭代,并且可以跳过范围检查并直接寻址每个元素。循环的每次迭代在 x86 上需要 3-5 条指令,只有一次内存查找。
当使用 IList 时,JIT 不知道底层集合的结构并最终使用 IEnumerator<int>
.循环的每次迭代使用两次接口(interface)调用 - 一次用于 Current
, 一个用于 MoveNext
(2-3 次内存查找和每次调用)。这很可能以大约 20 条非常昂贵的指令结束,您对此无能为力。
编辑:如果您对 JIT 从发布版本没有附加调试器发出的实际机器代码感到好奇,就在这里。
int s = 0;
00000000 push ebp
00000001 mov ebp,esp
00000003 push edi
00000004 push esi
00000005 xor esi,esi
foreach (int i in arg)
00000007 xor edx,edx
00000009 mov edi,dword ptr [ecx+4]
0000000c test edi,edi
0000000e jle 0000001B
00000010 mov eax,dword ptr [ecx+edx*4+8]
s += i;
00000014 add esi,eax
00000016 inc edx
foreach (int i in arg)
00000017 cmp edi,edx
00000019 jg 00000010
int s = 0;
00000000 push ebp
00000001 mov ebp,esp
00000003 push edi
00000004 push esi
00000005 push ebx
00000006 sub esp,1Ch
00000009 mov esi,ecx
0000000b lea edi,[ebp-28h]
0000000e mov ecx,6
00000013 xor eax,eax
00000015 rep stos dword ptr es:[edi]
00000017 mov ecx,esi
00000019 xor eax,eax
0000001b mov dword ptr [ebp-18h],eax
0000001e xor edx,edx
00000020 mov dword ptr [ebp-24h],edx
foreach (int i in arg)
00000023 call dword ptr ds:[009E0010h]
00000029 mov dword ptr [ebp-28h],eax
0000002c mov ecx,dword ptr [ebp-28h]
0000002f call dword ptr ds:[009E0090h]
00000035 test eax,eax
00000037 je 00000052
00000039 mov ecx,dword ptr [ebp-28h]
0000003c call dword ptr ds:[009E0110h]
s += i;
00000042 add dword ptr [ebp-24h],eax
foreach (int i in arg)
00000045 mov ecx,dword ptr [ebp-28h]
00000048 call dword ptr ds:[009E0090h]
0000004e test eax,eax
00000050 jne 00000039
00000052 mov dword ptr [ebp-1Ch],0
00000059 mov dword ptr [ebp-18h],0FCh
00000060 push 0F403BCh
00000065 jmp 00000067
00000067 cmp dword ptr [ebp-28h],0
0000006b je 00000076
0000006d mov ecx,dword ptr [ebp-28h]
00000070 call dword ptr ds:[009E0190h]
关于c# - 迭代 T[] 转换为 IList<T> 的开销,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8280377/
我想知道,通过数据 channel 发送数据时 WebRTC 会产生多少开销。 我知道 Websockets 每帧有 2 - 14 字节的开销。 WebRTC 是否使用更多开销?我在网上找不到一些有用
我想知道与创建新类而不是该类的新对象相关的开销是小还是大。我正在使用 dojo,但我将提供纯 JS 的示例。我将在启动时创建 10 到 100 个对象,我认为这不会是一个严重的问题,但我想涵盖所有基础
我有一个如下所示的表设置。 Table comment_flags user_id comment_id 我允许用户标记评论,然后给他们取消标记的选项,因为他们可能犯了一个错误。 问题
这个问题已经有答案了: 已关闭10 年前。 Possible Duplicate: In MySQL what does “Overhead” mean, what is bad about it,
我正在制作一个非常简单的游戏,只是为了好玩/练习,但无论它现在有多简单,我仍然想很好地编写它,以防我想回到它并只是为了学习 因此,在这种情况下,我的问题是: 对象分配涉及多少开销?解释器对此的优化程度
我有一些资源敏感的东西要写。我想知道与仅将这些变量一起传递(例如作为函数参数)相比,在结构中将变量组合在一起是否真的会导致内存开销。 如果是这样,那么在不产生开销的情况下创建对惰性值进行操作的东西的好
我一直在开发一个实时应用程序,并注意到一些 OOP 设计模式在 Python 中引入了难以置信的开销(使用 2.7.5 进行了测试)。 直截了当,当字典被另一个对象封装时,为什么简单的字典值访问器方法
我正在从 ifstream 中读取随机 ascii 文本文件。我需要能够将整个消息放入字符串类型以进行字符解析。我当前的解决方案有效,但我认为我通过使用等效于此的方式来谋杀更冗长文件的处理时间: st
纯粹从软件工程的角度来看,getActivity() 有多少开销? 我在整个应用程序中经常多次使用此方法,并考虑使用一个引用 getActivity() 的全局变量。 如果为 Activity 设置一
我一直在研究 Riccardo Terrell 的 Akka.NET 分形演示 (https://github.com/rikace/akkafractal) 以尝试理解它。 (这很棒,顺便说一句)
我正在尝试使用高分辨率计时器查找我的代码运行时间,我注意到计时器的结果不一致,我想知道为什么会这样。 我找到了这篇文章 How do you test running time of VBA code
我正在学习WPF。我现在开始装订了。使用 INotifyPropertyChanged 时绑定(bind)是否依赖反射?是这样,价格是多少?我正在考虑使用 WPF 来显示通过 UDP 流式传输的数据,
我有某种模板化基类 template class Base { }; 并希望将其派生实例存储在列表中。为此,我使用 using derived_handle = std::unique_ptr v
使用GHC.TypeLits中的Sing有任何开销吗? ?以程序为例: {-# LANGUAGE DataKinds #-} module Test (test) where import GHC.T
我有某种模板化基类 template class Base { }; 并希望将其派生实例存储在列表中。为此,我使用 using derived_handle = std::unique_ptr v
我有一个 ORM sqlalchemy 模型,我需要构建一个查询(使用 ORM 类更容易构建),但这需要大量时间。当我直接像 SQL 一样向数据库执行相同的查询时,速度相当快。 使用 SQLAlche
我在 PHP 平台上有一家商店(开发不善),那里有很多不好的查询(没有索引的长查询、rand() 排序、动态计数,..) 我现在无法更改查询,但我必须调整服务器才能保持事件状态。 我尝试了我所知道的一
我有一个使用 JQuery mobile 构建的移动应用程序,响应时间对我来说非常重要,因为我希望为我的用户提供流畅的体验。 我刚刚将网站的安装移至本地服务器,以提高应用程序的性能,因为它连接到本地
关于数据库设计的问题。如果我有 28 个 bool 值并且能够将它们添加为每行 28 个 bool 值或一个整数,哪一个会更快?哪种方法将使磁盘上的表大小保持最低? 这是在假设我需要的可以通过查询中的
我有一个看起来像 Boost.Array 的简单类。有两个模板参数 T 和 N。Boost.Array 的一个缺点是,每个使用这种数组的方法都必须是带有参数 N 的模板(T 可以)。结果是整个程序往往
我是一名优秀的程序员,十分优秀!