当我简单地包装 DataStructure 调用时,我看不到 AggressiveInlining 的效果
例如:
public class WList
{
//made static just to check inlining
//otherwise inlining might not kick in since 'this' keyword will disallow it
static System.Collections.ArrayList list;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override void Put(object item)
{
WList.list.Add(item);
}
}
然后这样做
//Add 1000000 number of items in the list
//Run this test many times
for (int j = 0; j < 100; j++)
{
Stopwatch sw = new Stopwatch();
sw.Start();
for (int i = 0; i < length; i++)
{
//ADD ITEMS IN A SIMPLE ARRAY LIST
arraylist.Add(emptyobject);
}
sw.Stop();
//clear the list
}
//Do similar test on wrapper
for (int j = 0; j < 100; j++)
{
Stopwatch sw = new Stopwatch();
sw.Start();
for (int i = 0; i < length; i++)
{
//ADD ITEMS IN ARRAY LIST THROUGH WRAPPER
wrapperList.Put(emptyobject);
}
sw.Stop();
//clear the list
}
Inlinig 基本上应该解开函数并将内部代码放在外面,但是我从包装器中获得的数字比直接调用要慢。
如果使用 Hastable 或使用 get 调用而不是 add,则数字保持不变。
函数调用的性能成本仍然存在。