一、简介
Julia(https://julialang.org/) 是一个面向科学计算的高性能动态高级程序设计语言。
Julia 最初是为了满足高性能数值分析和计算科学的需要而设计的,不需要解释器,速度快。
优势
- 性能天花板:媲美 C/Fortran,告别 "两语言鸿沟"。这是 Julia 最核心卖点,解决了科学计算领域长久的痛点:
- Python、Matlab 写业务逻辑简单,但循环、数值运算极慢;C/C++ 速度快但开发繁琐、语法厚重,工程上常要「Python 写上层 + C 写底层」两套代码,维护成本极高。
- Julia 基于LLVM 即时编译 (JIT),代码运行前编译为机器码,原生循环、矩阵运算速度接近 C、Fortran,无需手动写底层扩展。
- 原生精细数值类型支持(正好对应你截图里的 Int8/UInt8/Int16...Int128/UInt128/Bool),定点、无符号、
- 超大整数零开销,高精度数值计算无溢出隐式转换坑。示例:百万次循环求和,Python 要几秒,Julia 微秒级完成。
- 专为科学、数值计算原生设计
- 顶级矩阵 / 线性代数支持
- 完善高精度与多精度数值
- 丰富科学计算生态
- 原生并行、分布式、GPU 加速
- 无缝调用其他语言,兼容现有生态
安装
从 https://julialang.org/downloads/ 下载 Windows Julia 通过 MSIX App Installer. 安装程序。默认安装在 C:\Users\xxx\AppData\Local\Programs目录下。


交互式命令窗口
终端中输入 julia 进入,执行julia命令和函数。

.jl文件


二、基础语法
数据类型
- 整数类型:Int8、Int16、Int32、Int64、Int128、UInt8、UInt16、UInt32、UInt64、UInt128
- 浮点类型:Float16、Float32、Float64
- 字符串:String
- Bool
python
a= """多行字符串 "RUNOOB",包含了单个引号"""
数组
python
# 一维数组
arr = [1,2,3]
# 不同类型
arr =[1, "RUNOOB", 2.5, pi]
# 指定类型
arr = Int64[1,2,3]
arr2 = String["Taobao","RUNOOB","GOOGLE"]
arr2[1]
# 指定数组类型及维度
array = Array{Int64}(undef, 3) # 表示一维数组,数组有 3 个元素
array = Array{Int64}(undef, 3, 3, 3) # 表示 3 维数组,每个维度数组有 3 个元素
# 二维数组
[1 2; 3 4]
[1:2 3:4]
元组
python
tupl=(5,10,15,20,25,30) # 创建一个元组
tupl = ((1,2),(3,4)) # 创建二维元组
# 元组命名
names_shape = (:corner1, :corner2)
values_shape = ((100, 100), (200, 200))
shape_item2 = NamedTuple{names_shape}(values_shape)
shape_item2.corner1
shape_item2.corner2
字典
python
julia> D=Dict("A"=>1, "B"=>2)
Dict{String, Int64} with 2 entries:
"B" => 2
"A" => 1
first_dict = Dict(string(x) => sind(x) for x = 0:5:360)
haskey(first_dict, "A")
D["C"] = 3
keys(first_dict)
values(first_dict)
for kv in first_dict
println(kv)
end
集合
python
var_site = Set()
push!(var_site, "Wiki")
in("Runoob", var_site)
A = Set{String}(["red","green","blue", "black"])
B = Set(["red","orange","yellow","green","blue","indigo","violet"])
# 并集
union(A, B)
# 交集
intersect(A, B)
# 差集
setdiff(A, B)

日期和时间
Date:表示日期,精确到日,只显示日期。
DateTime:表示日期和时间,精确到毫秒。
DateTime:表示日时间,精确到纳秒,代表一天 24 小时中的特定时刻。
python
import Dates
julia> import Dates
julia> rightnow = Dates.Time(Dates.now()) # 时间
08:41:15.917
julia> theday = Dates.Date(2022,5,6) # 日期
2022-05-06
julia> today_date = Dates.today()
2022-05-11
julia> Dates.now(Dates.UTC)
2022-05-11T00:44:20.136
# 格式化时间
julia> Dates.DateTime("20220429 120000", "yyyymmdd HHMMSS")
2022-04-29T12:00:00
julia> Dates.DateTime("19/04/2022 17:42", "dd/mm/yyyy HH:MM")
2022-04-19T17:42:00
三、函数
python
function functionname(args)
expression
expression
expression
...
expression
end
python
function f(x,y)
x + y
end
f(2,3)
四、流程控制
- 复合表达式:begin 和 ;。
- 条件表达式:if-elseif-else 和 ?: (三元运算符)。
- 短路运算:逻辑运算符 &&(与)和 ||(或),以及链式比较。
- 循环语句:循环:while 和 for。
- 异常处理:try-catch、error 和 throw。
begin ... end 表达式可以按顺序计算若干子表达式,并返回最后一个子表达式的值:
python
julia> z = begin
x = 1
y = 2
x + y
end
3
python
if boolean_expression 1
/* 当布尔表达式 1 为真时执行 */
elseif boolean_expression 2
/* 当布尔表达式 2 为真时执行 */
elseif boolean_expression 3
/* 当布尔表达式 3 为真时执行 */
else
/* 当上面条件都不为真时执行 */
if x < y
println("x is less than y")
elseif x > y
println("x is greater than y")
else
println("x is equal to y")
end
三元运算符
python
julia> x = 1; y = 2;
julia> println(x < y ? "less than" : "not less than")
less than
julia> x = 1; y = 0;
julia> println(x < y ? "less than" : "not less than")
not less than
Julia 中的 && 和 || 运算符分别对应于逻辑 "与" 和 "或" 操作。
在表达式 a && b 中,子表达式 b 仅当 a 为 true 的时候才会被执行,如果 a 为 false 则直接返回 false。
在表达式 a || b 中,子表达式 b 仅在 a 为 false 的时候才会被执行,如果 a 为 true ,直接返回 a。
python
julia> i = 1;
julia> while i <= 5
println(i)
global i += 1
end
1
2
3
4
5
python
julia> for i = 1:5
println(i)
end
1
2
3
4
5
python
julia> for i in [1,4,0]
println(i)
end
1
4
0
julia> for s ∈ ["foo","bar","baz"]
println(s)
end
foo
bar
baz
python
julia> i = 1;
julia> while true
println(i)
if i >= 5
break
end
global i += 1
end
1
2
3
4
5
julia> for j = 1:1000
println(j)
if j >= 5
break
end
end
1
2
3
4
5
推导式
python
[表达式 for 变量 in 列表]
[out_exp_res for out_exp in input_list]
或者
[表达式 for 变量 in 列表 if 条件]
[out_exp_res for out_exp in input_list if condition]
julia> [X^2 for X in 1:5]
5-element Array{Int64,1}:
1
4
9
16
25
遍历数组
python
julia> a = ["a", "b", "c"];
julia> for (index, value) in enumerate(a)
println("$index $value")
end
1 a
2 b
3 c
julia> arr = rand(0:9, 4, 4)
4×4 Array{Int64,2}:
7 6 5 8
8 6 9 4
6 3 0 7
2 3 2 4
julia> [x for x in enumerate(arr)]
4×4 Array{Tuple{Int64,Int64},2}:
(1, 7) (5, 6) (9, 5) (13, 8)
(2, 8) (6, 6) (10, 9) (14, 4)
(3, 6) (7, 3) (11, 0) (15, 7)
(4, 2) (8, 3) (12, 2) (16, 4)
异常处理
python
julia> try
sqrt("ten")
catch e
println("你需要输入一个数字")
end
你需要输入一个数字
f = open("file")
try
# operate on file f
finally
close(f)
end
julia> f(x) = x>=0 ? exp(-x) : throw(DomainError(x, "argument must be nonnegative"))
f (generic function with 1 method)
julia> f(1)
0.36787944117144233
julia> f(-1)
ERROR: DomainError with -1:
argument must be nonnegative
Stacktrace:
[1] f(::Int64) at ./none:1
五、文件读写
python
# 打开文件,如果文件不存在就创建 runoob-file.txt
io = open("runoob-file.txt", "w");
# 写入文件内容
write(io, "Hello world!\nRunoob Julia Test");
txt=readlines(file, keep=true)
# 关闭文件
close(io);
python
open("./runoob-test/julia/runoob-file.txt") do file
# 执行文件操作
end
python
# 打开文件
open("runoob-file.txt") do file
# 逐行读取文件内容
for ln in eachline(file)
# 输出字符串长度与字符串
println("$(length(ln)), $(ln)")
end
end