Performance penalty using anonymous function in Julia

前端 未结 3 682
北荒
北荒 2021-01-05 13:09

I have noticed that there is a performance penalty associated with using anonymous functions in Julia. To illustrate I have two implementations of quicksort (taken from the

3条回答
  •  庸人自扰
    2021-01-05 14:04

    As others have noted, the code you've written is idiomatic Julia and will someday be fast, but the compiler isn't quite there yet. Besides using FastAnonymous, another option is to pass types instead of anonymous functions. For this pattern, you define an immutable with no fields and a method (let's call it evaluate) that accepts an instance of the type and some arguments. Your sorting function would then accept an op object instead of a function and call evaluate(op, x, y) instead of op(x, y). Because functions are specialized on their input types, there is no runtime overhead to the abstraction. This is the basis for reductions and specification of sort order in the standard library, as well as NumericExtensions.

    For example:

    immutable AscendingSort; end
    evaluate(::AscendingSort, x, y) = x < y
    
    function qsort_generic!(a,lo,hi,op=AscendingSort())
        i, j = lo, hi
        while i < hi
            pivot = a[(lo+hi)>>>1]
            while i <= j
                while evaluate(op, a[i], pivot); i += 1; end
                while evaluate(op, pivot, a[j]); j -= 1; end
                if i <= j
                    a[i], a[j] = a[j], a[i]
                    i, j = i+1, j-1
                end
            end
            if lo < j; qsort_generic!(a,lo,j,op); end
            lo, j = i, hi
        end
        return a
    end
    

提交回复
热议问题