English 中文(简体)
优化法典 Tips:
原标题:Code Optimization Tips:

我正在使用以下的阿盟例行公事来布置一阵。 我想知道我法典的效率低下:

.386
.model flat, c
option casemap:none


.code
            public sample
            sample PROC
            ;[ebp+0Ch]Length
            ;[ebp+08h]Array
                            push ebp
                            mov ebp, esp
                            push ecx
                            push edx
                            push esi
                            push eax
                            mov ecx,[ebp+0Ch]
                            mov esi,[ebp+08h]
                _bubbleSort:
                            push ecx
                            push esi
                            cmp ecx,1
                            je _exitLoop
                            sub ecx,01h
                            _miniLoop:
                                        push ecx
                                        mov edx,DWORD PTR [esi+4]
                                        cmp DWORD PTR [esi],edx
                                        ja _swap
                                        jmp _continueLoop
                            _swap:      
                                        lodsd
                                        mov DWORD PTR [esi-4],edx
                                        xchg DWORD PTR [esi],eax    
                                        jmp _skipIncrementESI
                            _continueLoop:
                                        add esi,4
                            _skipIncrementESI:
                                        pop ecx
                                        loop _miniLoop 
                            _exitLoop:
                            pop esi
                            pop ecx 
                            loop _bubbleSort
                            pop eax
                            pop esi
                            pop edx
                            pop ecx
                            pop ebp
                            ret 
            sample ENDP
            END 

基本上,我有两处 lo,就泡沫算法而言,通常也是如此。 胎盘对外 lo的价值为10,对内 lo而言,为[Cx-1]。 我已尝试过例行工作,并汇编和成功运作,但我不相信这种做法是否有效。

最佳回答

几个简单的假设:

1) Try to minimize the number of conditional jumps, because they are very expensive. Unroll if possible. 2) Reorder instructions to minimize stalls because of data depencency:

cmp DWORD PTR [esi],edx ;// takes some time to compute,
mov edx,DWORD PTR [esi+4] ; 
ja _swap ;// waits for results of cmp

3) 避免旧复合指令(dec ,jnz pair 高于loop,且不受x 登记的约束

很难起草比优化汇编者所产生守则更快的《组装法》,因为你应考虑许多因素:数据和指示的大小、一致性、管道、教学时间。 http://agner.org/optimize/“rel=“nofollow”>。 我特别建议第一书:C++的优化软件。

问题回答

你们可以做几件事,以加快议会法典的步伐:

  • 不要像<代码>ja标签_1;jmp标签_2。 见jbe 标签_2

  • <代码>loop是一个非常缓慢的指示。 http://code>dec ebx;jnz loopstart

  • 利用所有登记册,而不是一再推波/波塞克斯和西。 使用<条码>bx和<条码>>。

  • jmp-targets should be well aligned. Use align 4 before the two loop-starts and after the jbe

Get yourself a manual for your cpu from Intel (you can download it as pdf), it has the timings for the opcodes, maybe it has other hints too.

如果我们不需要这一指示的旗帜,就给“额外,4”注入:

_continueLoop:
            lea esi,[esi+4]




相关问题
How do I sort enum members alphabetically in Java?

I have an enum class like the following: public enum Letter { OMEGA_LETTER("Omega"), GAMMA_LETTER("Gamma"), BETA_LETTER("Beta"), ALPHA_LETTER("Alpha"), private final String ...

Grokking Timsort

There s a (relatively) new sort on the block called Timsort. It s been used as Python s list.sort, and is now going to be the new Array.sort in Java 7. There s some documentation and a tiny Wikipedia ...

Sorting twodimensional Array in AS3

So, i have a two-dimensional Array of ID s and vote count - voteArray[i][0] = ID, voteArray[i][1] = vote count I want the top 3 voted items to be displayed in different colors, so i have a 2nd Array -...

Linq operations against a List of Hashtables?

I m working with a set of legacy DAO code that returns an IList, where each Hashtable represents the row of a dynamically executed SQL query. For example, the List might contain the following records/...

C++ Array Sort Me

Stuck on an array sorter. Have to sort numbers from largest to smallest. I m trying two loops (one nested in the other). Here s the code: int counter=0; // inner counter int counter2=0; // outer ...

Can I Nest OrderBy in .NET?

This doesn t seem to work as I intend. VB.NET: Dim x = Model.Discussions.OrderByDescending(Function(d) d.Messages.OrderByDescending(Function(m) m.Sent).First.Sent) For Each d As Discussion In x ....

sorting elements javascript

I m looking for a way to sort my elements, but it isn t as easy as it sounds. Please let me explain My elements are grouped per 6 elements (thumbnails), each x represents a thumbnail However all ...

热门标签