许多科学计算语言区分绝对时间(挂钟)和CPU时间(处理器周期).例如,在Matlab中我们有:
>> tic; pause(1); toc
Elapsed time is 1.009068 seconds.
>> start = cputime; pause(1); elapsed = cputime - start
elapsed =
0
Run Code Online (Sandbox Code Playgroud)
在Mathematica,我们有:
>>In[1]:= AbsoluteTiming[Pause[1]]
>>Out[1]= {1.0010572, Null}
>>In[2]:= Timing[Pause[1]]
>>Out[2]= {0., Null}
Run Code Online (Sandbox Code Playgroud)
当对计算服务器上运行的代码进行基准测试时,这种区别非常有用,其中绝对时序结果可能存在很大差异,具体取决于其他进程同时运行的情况.
茱莉亚标准库通过提供支持表达式的时机tic(),toc(),@time和一些其他的功能/宏都基于time_ns(),测量绝对时间的函数.
>>julia> @time sleep(1)
elapsed time: 1.017056895 seconds (135788 bytes allocated)
Run Code Online (Sandbox Code Playgroud)
我的问题:是否有一种简单的方法可以获得Julia中表达式评估所用的CPU时间?
(旁注:做一些挖掘,似乎Julia计时基于libuv的uv_hrtime()功能.在我看来,使用同一个库可能会提供一种方法来访问Julia中经过的CPU时间,但我不是专家.有没有人尝试过这样的东西?)uv_getrusage
我正在寻找一个Julia函数,当应用于模块名称时,它会列出通过模块可用的函数.
基本上,我不想浏览源代码,我注意到许多模块的文档通常都没有.
将rgb图像转换为灰度图像的两个函数:
function rgb2gray_loop{T<:FloatingPoint}(A::Array{T,3})
r,c = size(A)
gray = similar(A,r,c)
for i = 1:r
for j = 1:c
@inbounds gray[i,j] = 0.299*A[i,j,1] + 0.587*A[i,j,2] + 0.114 *A[i,j,3]
end
end
return gray
end
Run Code Online (Sandbox Code Playgroud)
和:
function rgb2gray_vec{T<:FloatingPoint}(A::Array{T,3})
gray = similar(A,size(A)[1:2]...)
gray = 0.299*A[:,:,1] + 0.587*A[:,:,2] + 0.114 *A[:,:,3]
return gray
end
Run Code Online (Sandbox Code Playgroud)
第一个是使用循环,而第二个使用矢量化.
在对它们进行基准测试时(使用Benchmark包),我得到了不同大小的输入图像的以下结果(f1是循环版本,f2矢量化版本):
A = rand(50,50,3):
| Row | Function | Average | Relative | Replications |
|-----|----------|-------------|----------|--------------|
| 1 | "f1" | 3.23746e-5 | 1.0 | 1000 …Run Code Online (Sandbox Code Playgroud) 我在Julia中创建了一个一维数组(矢量),即a=[1, 2, 3, 4, 5].然后我想创建一个新的向量b,其中b有完全相同的元素a,即b=[1, 2, 3, 4, 5].
似乎直接使用b = a只是为原始集合创建一个指针,这意味着如果我修改b并且a是可变的,修改也将反映在中a.例如,如果我使用!pop(b),那么b=[1, 2, 3, 4]和a=[1, 2, 3, 4].
我想知道是否有一个官方功能只是复制或克隆集合,这种变化b不会发生a.我找到了一个解决方案b = collect(a).我希望有人提供一些其他方法.
似乎朱莉娅的所有具体类型都是叶子类型,但反之则不然.例如,Type{Int64}是叶子类型,但它不具体:
julia> Type{Int64}.abstract
true
julia> Type{Int64}.isleaftype
true
Run Code Online (Sandbox Code Playgroud)
我的理解是,这是有道理的,因为没有类型的值Type{Int64}.类型Int64有具体类型DataType.但是,因为Type{Int64}没有非平凡的子类型,所以它被认为是叶子类型.
但是,文档isleaftype有点令人困惑:
isleaftype(T)
Determine whether T is a concrete type that can have instances, meaning its
only subtypes are itself and Union{} (but T itself is not Union{}).
Run Code Online (Sandbox Code Playgroud)
Type{Int64}不能有实例,所以第一句话表明它不是叶子类型.然而,确实它的唯一亚型本身就是Union{}这样,所以第二句话表明它是.
文档是否会混淆叶子类型和具体类型,如果是,那么哪个含义是正确的?
julia 中的“xor”运算符由以下符号定义:?
如果我没记错的话,这也是唯一代表“异或”的符号。
你应该如何方便地输入这个?
我应该将它复制或粘贴到我的代码中还是记住 unicode 表示?
我认为感叹号!是逻辑NOT运算符的符号。现在,在学习DataFrames包中的索引时,我遇到了这个:data[!,:Treatment]. 这似乎与使用已知的冒号符号相同:
data[:,:Treatment]==data[!,:Treatment]是true。
为什么会有这种冗余呢?
之间有什么区别
function foo(a::Adjoint{Float64, Matrix{T}} where T)
return 0
end
Run Code Online (Sandbox Code Playgroud)
和
function foo(a::Adjoint{Float64, Matrix{T} where T})
return 1
end
Run Code Online (Sandbox Code Playgroud)
(注意花括号的位置。)
julia> methods(foo)
# 2 methods for generic function "foo":
[1] foo(a::Adjoint{Float64,Array{T,2}} where T) in Main at REPL[247]:2
[2] foo(a::Adjoint{Float64,Array{T,2} where T}) in Main at REPL[248]:2
Run Code Online (Sandbox Code Playgroud)
似乎在这两种情况下,函数都会接受 Matrix 类型的伴随T?我无法弄清楚这两个功能之间的区别是什么。
有没有办法在不运行任意表达式的情况下运行名称解析?例如,我想采用诸如
quote
x = 1
y = 2*x + 1
z = x^2 - 1
f(x) = 2*x + 1
end
Run Code Online (Sandbox Code Playgroud)
并被告知在此块的范围内定义的名称是x, y, z, f,并且名称*, +, ^, -是从此块的范围之外拉入的。如果它可以告诉我在其主体中定义了一个子作用域,f它会创建自己的名称x并+从封闭的作用域中拉入,则加分。
这个问题出现在Julia Zulip 社区
So I read the documentation for using and import in Julia. However what this does not tell me is how I should be using these two statements in practice (and, given the lack of orthogonality, this is not too easy).
Case in point: let's put the following trivial code in "myfile.jl":
module MyModule
f() = 1
export f
end
import .MyModule # or: using .MyModule
Run Code Online (Sandbox Code Playgroud)
if I use import on the last line, then f is not exported to …
julia ×10
benchmarking ×1
collections ×1
cpu-time ×1
loops ×1
module ×1
package ×1
performance ×1
time ×1
types ×1
where-clause ×1
xor ×1