English 中文(简体)
不安全的业绩 C# , 清除了长时间,无法进入阵列指数
原标题:Unsafe performance C# , remove long being cast to int to access array index

在C#中,有固定阵列的守法

array[(int)(index)]

index is long (and has to be long for some reasons).. so I need to cast it to int in order to get access to array element. Is there a way to use pointers or some other combination of actions in unsafe code to prevent this cast cause index can have long value of 1 for example (and never exceeds int value obv).. I feel that maybe unsafe pointers addition could prevent it from happening. I also feel this cast is not for free..and performance is the issue here. (exactly this line) I also think its not possible but I ask just in case im mistaken.

奥基德提供一些细节,像在伊德里克长时期那样做一些必要的逻辑行动。

价值 = 阵列[(int)(preIndex &对面罩);

这些行动是必要的,法典已经相当低。 我只能认为最优化的是,从长期到现在,需要开展双向行动。

增 编

最佳回答

是的trivial,从long改为int,但我确信,a:won t matter in any sane Code and b: it is una (arrays areaccess through intindex>;期间)。 显示<>won t事项:

static class Program
{
    static int GetSize() { return 10000; }
    static void Main() {

        int size = GetSize();
        int[] someData = new int[size];

        var watchInt32 = Stopwatch.StartNew();
        for (int i = 0; i < 5000; i++)
        {
            for (int j = 0; j < size; j++)
            {
                someData[j]++;
            }
        }
        for (int j = 0; j < size; j++) someData[j] = 0;
        watchInt32.Stop();
        long lSize = size;
        var watchInt64 = Stopwatch.StartNew();
        for (int i = 0; i < 5000; i++)
        {
            for (long j = 0; j < lSize; j++)
            {
                someData[j]++;
            }
        }
        watchInt64.Stop();

        Console.WriteLine("{0}ms vs {1}ms over {2} iterations",
            (int)watchInt32.ElapsedMilliseconds,
            (int)watchInt64.ElapsedMilliseconds, 5000 * size);
    }
}

I get:

162ms v. 215ms over50000000 iterations

So unless your code does nothing except array access, this just isn t going to matter. At all. In any significant way.

问题回答

在<代码>unchecked的情形下(违约),所投的只是丢弃不需要的微型信贷机构,因此没有罚款。 如果指数范围超过<代码>int.MaxValue,则它将放弃一个例外。

I don t think you have to worry about a possible performance impact due to the conversion. Just look at the machine code generated by the JIT, they are identical for both the int and the long index:

页: 1 指数:

        var val = arr[idx];
00000059  cmp         ebx,dword ptr [edx+4] 
0000005c  jae         00000078 
0000005e  mov         esi,dword ptr [edx+ebx*4+8] 


x86, release mode, casted long index:

        var val = arr[(int)idx];
0000005f  cmp         ebx,dword ptr [edx+4] 
00000062  jae         00000081 
00000064  mov         esi,dword ptr [edx+ebx*4+8]

页: 1 指数:

        var val = arr[idx];
00000060  movsxd      rcx,ebx 
00000063  mov         rax,qword ptr [rdi+8] 
00000067  cmp         rcx,3 
0000006b  jae         0000000000000080 
0000006d  mov         ecx,dword ptr [rdi+rcx*4+10h] 


x64, release mode, long index:

        var val = arr[(int)idx];
00000061  movsxd      rcx,ebx 
00000064  mov         rax,qword ptr [rdi+8] 
00000068  cmp         rcx,3 
0000006c  jae         0000000000000080 
0000006e  mov         ecx,dword ptr [rdi+rcx*4+10h]

正如丹尼尔·吉里格指出的,《国际空间法》第5条指示并不需要在机器法中考虑,32个国际会计准则被简单放弃。

int count = 10000000;
            object [] a = new object[count];

            Stopwatch s1 = Stopwatch.StartNew();
            for (long i = 0; i < count; i++)
                a[i] = new object();
            s1.Stop();

            Stopwatch s2 = Stopwatch.StartNew();
            for (int i = 0; i < count; i++)
                a[i] = new object();
            s2.Stop();       



            Debug.WriteLine(s1.ElapsedTicks + "  " + s2.ElapsedTicks);

3362502 3115428

So there is no significant impact.

您从这一行文中看到的绩效问题来自指数调查,即它是一个固定阵列,如目标。 ? 或者你是否执行指数财产?





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签