确定javascript异步操作的结束(determining the end of asynchronous operations javascript)
如果我有一个传递了这个函数的函数:
function(work) { work(10); work(20); work(30); }
(可以有任意数量的
work
电话,其中包含任何数字。)
work
性能一些异步活动 - 比如,对于这个例子,它只是一个timeout
。 我完全可以控制完成此操作的工作(事实上,它的定义一般)。确定所有
work
通话何时完成的最佳方法是什么?
我的当前方法在调用工作时递增计数器,在完成时递减计数器,并在计数器为0时触发
all work done
事件(在每次递减后检查)。 但是,我担心这可能是某种竞争条件。 如果不是这样,请告诉我原因,这将是一个很好的答案。If I have a function that's passed this function:
function(work) { work(10); work(20); work(30); }
(There can be any number of
work
calls with any number in them.)
work
performance some asynchronous activity—say, for this example, it just is atimeout
. I have full control over whatwork
does on the completion of this operation (and, in fact, its definition in general).What's the best way of determining when all the calls to
work
are done?
My current method increments a counter when work is called and decrements it when it completes, and fires the
all work done
event when the counter is 0 (this is checked after every decrement). However, I worry that this could be a race condition of some sort. If that is not the case, do show my why and that would be a great answer.
原文:https://stackoverflow.com/questions/6852059
最满意答案
这是另一个版本,它带来了显着的改进:
function cohens_kappa2(x::Vector{Int}, k::Int) d = Dict{Int,Int}() n = length(x) c1 = Int[] pnew = 0 for i=1:n p = get(d,x[i],0) if p>0 c1[p] += 1 else pnew += 1 d[x[i]] = pnew push!(c1,1) end end c2 = zeros(Int,pnew) for i=(k+1):n if x[i-k]==x[i] c2[d[x[i]]] += 1 ; end end num, dentmp = 0.0, 0.0 for i=1:pnew pjjk = c2[i]/(n-k) pj = c1[i] / n num += pjjk - pj^2 dentmp += pj^2 end return (num / (1.0-dentmp)) end
一般来说,几乎总是可以进行优化,但就像从自然界中提取石油一样,它会增加程序员的成本和工作量。
在一个测试用例中,上面给了我5倍到10倍的加速。 您的数据结果如何?
Here is another version, which gives a significant improvement:
function cohens_kappa2(x::Vector{Int}, k::Int) d = Dict{Int,Int}() n = length(x) c1 = Int[] pnew = 0 for i=1:n p = get(d,x[i],0) if p>0 c1[p] += 1 else pnew += 1 d[x[i]] = pnew push!(c1,1) end end c2 = zeros(Int,pnew) for i=(k+1):n if x[i-k]==x[i] c2[d[x[i]]] += 1 ; end end num, dentmp = 0.0, 0.0 for i=1:pnew pjjk = c2[i]/(n-k) pj = c1[i] / n num += pjjk - pj^2 dentmp += pj^2 end return (num / (1.0-dentmp)) end
In general, optimization is almost always possible, but like extracting oil from nature, it comes with growing costs and effort for the programmer.
On a test case the above gave me 5x to 10x speedup. What is the result for your data?
相关问答
更多-
朱莉娅:GLM情节logit功能(Julia: GLM plot logit function)[2023-04-20]
using Plots plot(x, predict(fit1), seriestype = :line) #where x is your predictor variable 更新以包含@pkofod的评论 using Plots plot(x, predict(fit1), seriestype = :line) #where x is your predictor variable Updated to include @pkofod 's comment -
朱莉娅的总和表示法?(Sum notation in julia?)[2022-05-15]
如果你在谈论普通的朱莉娅变量 c = rand(5,3) x = rand(5,3) @show sum(c.*x) 但是,如果你指的是JuMP(根据你以前的问题),那么使用sum{} : using JuMP m = Model() @variable(m, 0 <= x[i=1:5,j=1:3] <= 1) c = rand(5,3) @constraint(m, sum{c[i,j]*x[i,j],i=1:5,j=1:3} <= 10) If you are talking about plain ... -
目前,Julia JIT在启动时编译了整个标准库。 我们意识到这种情况,目前正在缓存LLVM JIT输出以补救这种情况,但直到那时,它就没有办法了(除了使用REPL)。 At the moment Julia JIT compiles its entire standard library on startup. We are aware of the situation and are currently working on caching the LLVM JIT output to remedy t ...
-
朱莉娅ccall windows(Julia ccall windows)[2023-10-20]
很好找到问题...我的dll文件是32和julia是64.所以要么安装32位版本的Julia或获得64位版本的.dll文件来推进。 函数按路径查找文件: ccall( (:fun, "C:\\Users\\pinq-\\Documents\\Julia\\librarie"), Int32, ()) Well found the problem... My dll file was 32 and julia was 64. So either install 32-bit version of Julia ... -
加速朱莉娅的kNN函数(Speeding up kNN function in Julia)[2023-05-28]
因此,首先,我删除了plot_probs的最后两行 - 绘图对于我认为的基准测试来说并不是一件好事,而且它基本上超出了我的(或您的)控制范围 - 如果它是一个真正的因素,可以尝试PyPlot。 我还plot_probs了几次plot_probs ,看看第一次花多少时间编译它: **********elapsed time: 1.071184218 seconds (473218720 bytes allocated, 26.36% gc time) **********elapsed time: 0.658 ... -
朱莉娅:优化功能(Julia: optimize function)[2023-07-08]
这是另一个版本,它带来了显着的改进: function cohens_kappa2(x::Vector{Int}, k::Int) d = Dict{Int,Int}() n = length(x) c1 = Int[] pnew = 0 for i=1:n p = get(d,x[i],0) if p>0 c1[p] += 1 else pnew += 1 ... -
朱莉娅用了什么方法?(What method did Julia use?)[2021-11-03]
@which正在寻找你: @which (1:10) * ones(1, 10) # *(A::AbstractArray{T,1}, B::AbstractArray{T,2}) at linalg/matmul.jl:89 在Jupyter中 ,它还将超链接到相应的代码行,其中该方法在Julia的GitHub中定义。 @which is what you are looking for: @which (1:10) * ones(1, 10) # *(A::AbstractArray{T,1}, B: ... -
来自http://docs.julialang.org/en/release-0.4/manual/constructors/ 内部构造方法 虽然外部构造方法成功地解决了为构造对象提供额外的便利方法的问题,但是它们无法解决本章介绍中提到的另外两个用例:强制不变量,并允许构造自引用对象。 对于这些问题,需要内部构造方法。 内部构造函数方法很像外部构造函数方法,有两个不同之处: 它在类型声明的块中声明,而不是像普通方法一样在它之外声明。 它可以访问一个名为new的特殊本地存在函数,该函数创建块类型的对象。 Fr ...
-
朱莉娅:如何使用类型(Julia: how to use type)[2023-07-11]
Julia中的函数返回它们中的最后一个表达式。 在这种情况下,它是data = 1 ,即返回data而不是MyData的新实例。 只需在data = 1之后添加new(data)行以返回MyData新实例,它就能正常工作。 Functions in Julia return the last expression in them. In this case, it is data = 1, that is data is returned instead of new instance of MyData. ... -
使用Iterators包,它可能如下所示: using Iterators for p in product([1,2], [3,4]) println(p) end 在哪里用算法替换println 。 如果获得所有组合的集合很重要,您也可以使用collect 。 Using the Iterators package, it might look like this: using Iterators for p in product([1,2], [3,4]) println(p) en ...