如何在Julia

时间:2018-09-27 00:30:04

标签: julia

question显示了如何在Python中重复字符串中的各个字符。

>>> s = '123abc'
>>> n = 3
>>> ''.join([c*n for c in s])
'111222333aaabbbccc'

您将如何在Julia中做到这一点?

编辑

作为Julia的新手,我为该语言所提供的功能感到惊讶。

例如,我以为上面的Python代码与任何语言的代码一样简单。但是,如下面我的回答所示,可以说茱莉亚等效代码join([c^n for c in s])更简单,并且对于任何一种语言,都可能达到最佳的简化程度。

另一方面,@ niczky12表明,在string函数中添加省略号运算符后,速度可以大大提高,而join函数却可以实现。 >

在一种情况下,朱莉娅为简单起见而发光。在另一种情况下,朱莉娅为速度而发光。

对于Python程序员,当他们注意到c^n在Python中只是c*n时,第一种情况应该几乎可以立即阅读。当他们使用...省略号运算符看到速度提高时,额外的复杂性可能不会阻止他们学习Julia。读者可能开始认为我希望许多Python程序员会认真对待Julia。他们不会错。

感谢@ rickhg12hs建议进行基准测试。我学到了很多东西。

3 个答案:

答案 0 :(得分:1)

您可以使用Julia理解程序或生成器来实现。

julia> VERSION
v"1.0.0"

julia> s = "123abc"
"123abc"

# n is number of times to repeat each character.
julia> n = 3
3

# Using a Julia comprehension with [...]
julia> join([c^n for c in s])
"111222333aaabbbccc"

# Using a Julia generator without the [...]
julia> join(c^n for c in s)
"111222333aaabbbccc"

对于小的弦,速度上的实际差异应该很小。

修改

TL; DR:通常,生成器比理解速度要快一些。但是,相反的情况请参见情况3。内存估计非常相似。

@ rickhg12hs建议拥有基准会很好。

使用出色的BenchmarkTools软件包,结果如下。

n = the number of times to repeat each character

s = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" in each case

在每种情况下,理解中位数时间C均排名第一,而生成器中位数时间G则排名第二。时间在适当的时候四舍五入,原始数字在数字摘要下方。当然,越小越好。

内存估计没有太大差异。

1。 n = 26,C = 3.8与G = 2.8μs,G更快

julia> using BenchmarkTools

julia> n = 26;

julia> @benchmark join([c^n for c in s])
BenchmarkTools.Trial:
  memory estimate:  3.55 KiB
  allocs estimate:  39
  --------------
  minimum time:     3.688 μs (0.00% GC)
  median time:      3.849 μs (0.00% GC)
  mean time:        4.956 μs (16.27% GC)
  maximum time:     5.211 ms (99.85% GC)
  --------------
  samples:          10000
  evals/sample:     8

julia> @benchmark join(c^n for c in s)
BenchmarkTools.Trial:
  memory estimate:  3.19 KiB
  allocs estimate:  36
  --------------
  minimum time:     2.661 μs (0.00% GC)
  median time:      2.756 μs (0.00% GC)
  mean time:        3.622 μs (19.94% GC)
  maximum time:     4.638 ms (99.89% GC)
  --------------
  samples:          10000
  evals/sample:     9

2。 n = 260,C = 10.7与G = 8.1μs,G更快

julia> n = 260;

julia> @benchmark join([c^n for c in s])
BenchmarkTools.Trial:
  memory estimate:  19.23 KiB
  allocs estimate:  39
  --------------
  minimum time:     8.125 μs (0.00% GC)
  median time:      10.691 μs (0.00% GC)
  mean time:        18.559 μs (35.36% GC)
  maximum time:     43.930 ms (99.92% GC)
  --------------
  samples:          10000
  evals/sample:     1

julia> @benchmark join(c^n for c in s)
BenchmarkTools.Trial:
  memory estimate:  18.88 KiB
  allocs estimate:  36
  --------------
  minimum time:     7.270 μs (0.00% GC)
  median time:      8.126 μs (0.00% GC)
  mean time:        10.872 μs (18.04% GC)
  maximum time:     10.592 ms (99.87% GC)
  --------------
  samples:          10000
  evals/sample:     4

3。 n = 2,600,C = 62.3与G = 63.7μs,C更快

julia> n = 2600; 

julia> @benchmark join([c^n for c in s])
BenchmarkTools.Trial:
  memory estimate:  150.16 KiB
  allocs estimate:  39
  --------------
  minimum time:     51.746 μs (0.00% GC)
  median time:      63.293 μs (0.00% GC)
  mean time:        77.315 μs (2.79% GC)
  maximum time:     3.721 ms (96.85% GC)
  --------------
  samples:          10000
  evals/sample:     1

julia> @benchmark join(c^n for c in s)
BenchmarkTools.Trial:
  memory estimate:  149.80 KiB
  allocs estimate:  36
  --------------
  minimum time:     47.897 μs (0.00% GC)
  median time:      63.720 μs (0.00% GC)
  mean time:        88.716 μs (17.58% GC)
  maximum time:     42.457 ms (99.83% GC)
  --------------
  samples:          10000
  evals/sample:     1

4。 n = 26,000,C = 667与G = 516μs,G更快

julia> n = 26000; 

julia> @benchmark join([c^n for c in s])
BenchmarkTools.Trial:
  memory estimate:  1.44 MiB
  allocs estimate:  39
  --------------
  minimum time:     457.589 μs (0.00% GC)
  median time:      666.710 μs (0.00% GC)
  mean time:        729.592 μs (10.91% GC)
  maximum time:     42.673 ms (98.76% GC)
  --------------
  samples:          6659
  evals/sample:     1

julia> @benchmark join(c^n for c in s)
BenchmarkTools.Trial:
  memory estimate:  1.44 MiB
  allocs estimate:  36
  --------------
  minimum time:     475.977 μs (0.00% GC)
  median time:      516.176 μs (0.00% GC)
  mean time:        659.001 μs (10.36% GC)
  maximum time:     42.268 ms (98.41% GC)
  --------------
  samples:          7548
  evals/sample:     1

答案 1 :(得分:1)

除了上述答案外,我发现string函数的运行速度甚至更快。这是我的基准:

julia> n = 2;

julia> s = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

julia> string((c^n for c in s)...) # proof that it works
"AABBCCDDEEFFGGHHIIJJKKLLMMNNOOPPQQRRSSTTUUVVWWXXYYZZ"

julia> n = 26000;

julia> @benchmark join(c^n for c in s)
BenchmarkTools.Trial:
  memory estimate:  1.44 MiB
  allocs estimate:  36
  --------------
  minimum time:     390.616 μs (0.00% GC)
  median time:      425.861 μs (0.00% GC)
  mean time:        484.638 μs (6.54% GC)
  maximum time:     45.006 ms (98.99% GC)
  --------------
  samples:          10000
  evals/sample:     1

julia> @benchmark string((c^n for c in s)...)
BenchmarkTools.Trial:
  memory estimate:  1.29 MiB
  allocs estimate:  31
  --------------
  minimum time:     77.480 μs (0.00% GC)
  median time:      101.667 μs (0.00% GC)
  mean time:        126.455 μs (0.00% GC)
  maximum time:     832.524 μs (0.00% GC)
  --------------
  samples:          10000
  evals/sample:     1

如您所见,它比@Julia Learner提出的join解决方案快大约3倍。 我在0.7上测试了上述内容,但没有弃用警告,因此我假设它在1.0上也能正常工作。甚至是TIO says so

答案 2 :(得分:0)

.catch(error =>{ if(axios.isCancel(error)){ // How to know if the cancel stops already? } }); 中测试过的代码。

我尝试写Version 1.0.0 (2018-08-08)时遇到错误。

map(x -> x^3, "123abc")

所以,还有另一种方法。

julia> map(x -> x^3, "123abc")
ERROR: ArgumentError: map(f, s::AbstractString) requires f to return AbstractChar; try map(f, collect(s)) or a comprehension instead

也许julia> map(x -> x^3, collect("123abc")) 6-element Array{String,1}: "111" "222" "333" "aaa" "bbb" "ccc" julia> join(map(x -> x^3, collect("123abc"))) "111222333aaabbbccc" 更方便。

repeat