Optimization of the compiler / interpreter Julia vs Python

I recently asked the following question about Python: Optimizing the interpreter in Python

Say I have a string in x, is the Python interpreter smart enough that what string.replace(x, x)needs to be converted to NOP?

The answer seems to be No (although the Python interpreter is capable of doing some optimizations through the spy optimizer).

I don't know what the equivalent expression is in Julia, but can Julia optimize these types with respect to the obvious statements?

+4
source share
1 answer

Based on the compiler

, Julia LLVM, ?

, code_native. , x ,

julia> f()=(x=7*24*60*60)
f (generic function with 1 method)

julia> code_native(f,())
    .section    __TEXT,__text,regular,pure_instructions
Filename: none
Source line: 1
    push RBP
    mov  RBP, RSP
    mov  EAX, 604800
Source line: 1
    pop RBP
    ret

, . , Any .

, y 256, , 1 , 256, , 1.

I

julia> g(y::Int16)=(y<256?1:0)
g (generic function with 1 method)

julia> code_native(g,(Int16,))
    .section    __TEXT,__text,regular,pure_instructions
Filename: none
Source line: 1
    push RBP
    mov  RBP, RSP
    cmp  DI, 256
Source line: 1 
    setl  AL
    movzx EAX, AL
    pop   RBP
    ret

II

julia> g(y::Int8)=(y<256?1:0)
g (generic function with 2 methods)

julia> code_native(g,(Int8,))
    .section    __TEXT,__text,regular,pure_instructions
Filename: none
    Source line: 1
    push RBP
    mov  RBP, RSP
    mov  EAX, 1
Source line: 1
    pop  RBP
    ret
+4

All Articles