1

When I tried to create my own alternative to classic array, I saw, that into disassembly code added one instruction: mov edx,dword ptr [myarray]. Why this additional instruction was added?

I want to use my functionality of my alternative, but do not want to lose performance! How to resolve this question? Every processor cycle is important for this application.

For example:

for (unsigned i = 0; i < 10; ++i)
{
    array1[i] = i;
    array2[i] = 10 - i;
}

Assembly (classic int arrays):

mov edx, dword ptr [ebp-480h]  
mov eax, dword ptr [ebp-480h]  
mov dword ptr array1[edx*4], eax  

mov ecx, 10
sub ecx, dword ptr [ebp-480h]
mov edx, dword ptr [ebp-480h]
mov dword ptr array2[edx*4], ecx

Assembly (my class):

mov edx,dword ptr [array1]
mov eax,dword ptr [ebp-43Ch]
mov ecx,dword ptr [ebp-43Ch]
mov dword ptr [edx+eax*4], ecx

mov edx, 10
sub edx, dword ptr [ebp-43Ch]
mov eax, dword ptr [array2]
mov ecx, dword ptr [ebp-43Ch]
mov dword ptr [eax+ecx*4], edx
5
  • one instruction is a loss of performance?? Commented Nov 10, 2012 at 17:35
  • 4
    I'm not sure anyone can say what why that "extra" instruction was added without seeing your code and what you're comparing it to. Commented Nov 10, 2012 at 17:36
  • I'm not sure, but size of code must be a constant (without additional funtionality). Commented Nov 10, 2012 at 17:37
  • 2
    Let me know how you get on optimizing C arrays, I'm genuinely interested. Commented Nov 10, 2012 at 17:37
  • Updated. I do not want to optimize. Size of code must be a constant (for operator []). Commented Nov 10, 2012 at 17:45

1 Answer 1

3

One instruction is not a loss of performance with today's processors. I would not worry about it and instead suggest you read Coding Horror's article on micro optimization.

However, that instruction is just moving the first index (myarray+0) to edx so it can be used.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.