# 一些提高代码性能的建议

#1

Python里面我们喜欢直接在全局变量里面写东西

``````import numpy as np

n = 1000
y = 200

A = np.random.rand(1000)

for i in range(1000):
A[i] = i%y

# etc.
``````

``````function main(params...)
for i  in 1:1000
do_something(params...)
end
end

function do_something(params...)
# do something
end

# 然后调用开始运行
main(1, rand(1000))
``````

#2

``````def foo(x):
if x is None:
print('none')
else:
print(x)
``````

``````foo(x::Nothing) = print("none")
foo(x) = print(x)
``````

#3

mark 正在学习中

#4

C++里面不也可以这样吗,定义的两个函数虽然同名但是传进去参数类型不同,不就可以定义不同的函数吗. 就像加法,整型和浮点型加法的过程虽然不同,但是他们都可以通过" +" 来计算.
Mathematica也是自带这个特点, f[x_?NumberQ]=… 和f[x_?SymbolQ] 也是接受不同类型的参数,从而执行不同的过程

#5

#6

function和method在Julia里和傳統OOP的含義不大一樣。需要慢慢理解。

Thus, the overall behavior of a function is a patchwork of the behaviors of its various method definitions. If the patchwork is well designed, even though the implementations of the methods may be quite different, the outward behavior of the function will appear seamless and consistent.

https://juliacn.github.io/JuliaZH.jl/latest/manual/methods/index.html

#7

c++还要考虑class method和继承的问题，所以c++的`multi dispatch`一般都是通过一些设计模式实现的。

#8

#9

#10

#12

``````foo(x::Nothing) = print("none")
foo(x) = print(x)
``````

``````foo(x::Int) = x+1
foo(x::Float) = y+2.0
a = foo(1)
b = foo(2.0) #都可以静态的推出
#对应于Python的推不出来的版本
def foo(x)：
if isinstance(x,int):
return x+1
if isintance(x,float):
return x+2.0
``````

#13

#14

#16

#17
``````foo(x::Float64) = x+2.0
``````