- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我一直在研究 Riccardo Terrell 的 Akka.NET 分形演示 (https://github.com/rikace/akkafractal) 以尝试理解它。 (这很棒,顺便说一句)
作为对自己的一个小挑战,我尝试的一件事是以更实用的方式重写它的一些部分。我已经让它工作了,但它比原来的慢很多。
这是(或多或少)为测试而改编的原始 Mandelbrot 集计算:
let mandelbrotSet (xp : int) (yp : int) (w : int) (h :int) (width : int) (height : int)
(maxr : float) (minr : float) (maxi : float) (mini : float) : List<int> =
let mutable zx = 0.
let mutable zy = 0.
let mutable cx = 0.
let mutable cy = 0.
let mutable xjump = ((maxr - minr) / ( float width))
let yjump = ((maxi - mini) / (float height))
let mutable tempzx = 0.
let loopmax = 1000
let mutable loopgo = 0
let outputList: int list = List.empty
for x = xp to (xp + w) - 1 do
cx <- (xjump * float x) - abs(minr)
for y = yp to (yp + h) - 1 do
zx <- 0.
zy <- 0.
cy <- (yjump * float y) - abs(mini)
loopgo <- 0
while (zx * zx + zy * zy <= 4. && loopgo < loopmax) do
loopgo <- loopgo + 1
tempzx <- zx
zx <- (zx * zx) - (zy * zy) + cx
zy <- (2. * tempzx * zy) + cy
(List.append outputList [loopgo]) |> ignore
outputList
这是我使用递归 mbCalc 函数执行的版本:
let mandelbrotSetRec (xp : int) (yp : int) (w : int) (h :int) (width : int) (height : int)
(maxr : float) (minr : float) (maxi : float) (mini : float) : List<int> =
let xjump = ((maxr - minr) / (float width))
let yjump = ((maxi - mini) / (float height))
let loopMax = 1000
let outputList: int list = List.empty
let rec mbCalc(zx:float, zy:float, cx:float, cy:float, loopCount:int) =
match (zx * zx + zy * zy), loopCount with //The square of the magnitude of z
| a,b when a > 4. || b = loopMax -> loopCount
| _ -> mbCalc((zx * zx) - (zy * zy) + cx, (2. * zx * zy) + cy, cx, cy, loopCount+1) //iteration is the next value of z^2+c
[|0..w-1|] //For each x...
|> Array.map (fun x -> let cx = (xjump * float (x+xp) - abs(minr))
[|0..h-1|] ///and for each y...
|> Array.map (fun y -> let cy = (yjump * float (y+yp) - abs(mini))
let mbVal = mbCalc(0., 0., cx, cy,0) //Calculate the number of iterations to convergence (recursively)
List.append outputList [mbVal]))|>ignore
outputList
这是否只是预料之中的,毫无意义地加载一个具有大量递归调用的 Actor,还是我只是在做一些非常低效的事情?非常感谢收到任何指示!
如果你想运行它们,那么这里有一个小测试脚本:
let xp = 1500
let yp = 1500
let w = 200
let h = 200
let width = 4000
let height = 4000
let timer1 = new System.Diagnostics.Stopwatch()
timer1.Start()
let ref = mandelbrotSet xp yp w h width height 0.5 -2.5 1.5 -1.5
timer1.Stop()
let timer2 = new System.Diagnostics.Stopwatch()
timer2.Start()
let test = mandelbrotSetRec xp yp w h width height 0.5 -2.5 1.5 -1.5
timer2.Stop
timer1.ElapsedTicks;;
timer2.ElapsedTicks;;
ref = test;;
编辑:根据 Philip 在下面的回答,我快速(太快了!)添加了列表输出,以制作无需任何导入即可在脚本中运行的内容。这是返回图像的代码:
let mandelbrotSetRec (xp : int) (yp : int) (w : int) (h :int) (width : int) (height : int)
(maxr : float) (minr : float) (maxi : float) (mini : float) : Image<Rgba32> =
let img = new Image<Rgba32>(w, h)
let xjump = ((maxr - minr) / (float width))
let yjump = ((maxi - mini) / (float height))
let loopMax = 1000
//Precalculate the possible colour list
let palette = List.append ([0..loopMax - 1] |> List.map(fun c -> Rgba32(byte(c % 32 * 7), byte(c % 128 * 2), byte(c % 16 * 14)))) [Rgba32.Black]
let rec mbCalc(zx:float, zy:float, cx:float, cy:float, loopCount:int) =
match (zx * zx + zy * zy), loopCount with //The square of the magnitude of z
| a,b when a > 4. || b = loopMax -> loopCount
| _ -> mbCalc((zx * zx) - (zy * zy) + cx, (2. * zx * zy) + cy, cx, cy, loopCount+1) //iteration is the next value of z^2+c
[|0..w-1|] //For each x...
|> Array.map (fun x -> let cx = (xjump * float (x+xp) - abs(minr))
[|0..h-1|] ///and for each y...
|> Array.map (fun y -> let cy = (yjump * float (y+yp) - abs(mini))
let mbVal = mbCalc(0., 0., cx, cy,0) //Calculate the number of iterations to convergence (recursively)
img.[x,y] <- palette.[mbVal]))|>ignore
img
最佳答案
首先,两个函数都返回 []
因此即使计算正确,也不会返回 mandlebrot 集。 List.append
返回一个列表,它不会改变现有列表。
使用下面的快速 BenchmarkDotNet 程序,其中每个函数都在其自己的模块中:
open BenchmarkDotNet.Attributes
open BenchmarkDotNet.Running
open ActorTest
[<MemoryDiagnoser>]
type Bench() =
let xp = 1500
let yp = 1500
let w = 200
let h = 200
let width = 4000
let height = 4000
[<Benchmark(Baseline=true)>]
member _.Mutable() =
Mutable.mandelbrotSet xp yp w h width height 0.5 -2.5 1.5 -1.5
[<Benchmark>]
member _.Recursive() =
Recursive.mandelbrotSet xp yp w h width height 0.5 -2.5 1.5 -1.5
[<EntryPoint>]
let main argv =
let summary = BenchmarkRunner.Run<Bench>()
printfn "%A" summary
0 // return an integer exit code
您的代码给出了这些结果:
| Method | Mean | Error | StdDev | Ratio | RatioSD | Gen 0 | Gen 1 | Gen 2 | Allocated |
|---------- |---------:|----------:|----------:|------:|--------:|---------:|---------:|------:|----------:|
| Mutable | 1.356 ms | 0.0187 ms | 0.0166 ms | 1.00 | 0.00 | 406.2500 | - | - | 1.22 MB |
| Recursive | 2.558 ms | 0.0303 ms | 0.0283 ms | 1.89 | 0.03 | 613.2813 | 304.6875 | - | 2.13 MB |
我注意到您正在使用 Array.map
但是在任何地方都没有捕获结果,因此将其更改为 Array.iter
让您的代码几乎相同:
| Method | Mean | Error | StdDev | Ratio | Gen 0 | Gen 1 | Gen 2 | Allocated |
|---------- |---------:|----------:|----------:|------:|---------:|------:|------:|----------:|
| Mutable | 1.515 ms | 0.0107 ms | 0.0094 ms | 1.00 | 406.2500 | - | - | 1.22 MB |
| Recursive | 1.652 ms | 0.0114 ms | 0.0101 ms | 1.09 | 607.4219 | - | - | 1.82 MB |
这种差异可能可以通过映射调用完成的额外分配来解释。分配很昂贵,尤其是当它是较大的数组时,因此最好尽可能避免使用它们。确切的时间因机器而异,但我希望在使用 BenchmarkDotNet 时会有类似的前后比率。
这可能会通过避免列表分配和预分配您填写的列表或数组来进一步优化。迭代调用也是如此。还循环遍历 Span<'T>
将比数组更快,因为它省略了边界检查,但您可能必须大量更改代码的形状才能做到这一点。
最后,始终使用像 BenchmarkDotNet 这样的统计基准测试工具来衡量这样的微基准测试中的性能。快速脚本作为起点很好,但它们不能替代考虑机器执行时间可变性的工具。
关于recursion - F# 递归与迭代速度/开销,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59667046/
在本教程中,您将借助示例了解 JavaScript 中的递归。 递归是一个调用自身的过程。调用自身的函数称为递归函数。 递归函数的语法是: function recurse() {
我的类(class) MyClass 中有这段代码: public new MyClass this[int index] { get {
我目前有一个非常大的网站,大小约为 5GB,包含 60,000 个文件。当前主机在帮助我将站点转移到新主机方面并没有做太多事情,我想的是在我的新主机上制作一个简单的脚本以 FTP 到旧主机并下载整个
以下是我对 AP 计算机科学问题的改编。书上说应该打印00100123我认为它应该打印 0010012但下面的代码实际上打印了 3132123 这是怎么回事?而且它似乎没有任何停止条件?! publi
fun fact(x: Int): Int{ tailrec fun factTail(y: Int, z: Int): Int{ if (y == 0) return z
我正在尝试用c语言递归地创建线性链表,但继续坚持下去,代码无法正常工作,并出现错误“链接器工具错误 LNK2019”。可悲的是我不明白发生了什么事。这是我的代码。 感谢您提前提供的大力帮助。 #inc
我正在练习递归。从概念上讲,我理解这应该如何工作(见下文),但我的代码不起作用。 请告诉我我做错了什么。并请解释您的代码的每个步骤及其工作原理。清晰的解释比只给我有效的代码要好十倍。 /* b
我有一个 ajax 调用,我想在完成解析并将结果动画化到页面中后调用它。这就是我陷入困境的地方。 我能记忆起这个功能,但它似乎没有考虑到动画的延迟。即控制台不断以疯狂的速度输出值。 我认为 setIn
有人愿意用通俗易懂的语言逐步解释这个程序(取自书籍教程)以帮助我理解递归吗? var reverseArray = function(x,indx,str) { return indx == 0 ?
目标是找出数组中整数的任意组合是否等于数组中的最大整数。 function ArrayAdditionI(arr) { arr.sort(function(a,b){ return a -
我在尝试获取 SQL 查询所需的所有数据时遇到一些重大问题。我对查询还很陌生,所以我会尽力尽可能地描述这一点。 我正在尝试使用 Wordpress 插件 NextGen Gallery 进行交叉查询。
虽然网上有很多关于递归的信息,但我还没有找到任何可以应用于我的问题的信息。我对编程还是很陌生,所以如果我的问题很微不足道,请原谅。 感谢您的帮助:) 这就是我想要的结果: listVariations
我一整天都在为以下问题而苦苦挣扎。我一开始就有问题。我不知道如何使用递归来解决这个特定问题。我将非常感谢您的帮助,因为我的期末考试还有几天。干杯 假设有一个包含“n”个元素的整数数组“a”。编写递归函
我有这个问题我想创建一个递归函数来计算所有可能的数字 (k>0),加上数字 1 或 2。数字 2 的示例我有两个可能性。 2 = 1+1 和 2 = 2 ,对于数字 3 两个 poss。 3 = 1+
目录 递归的基础 递归的底层实现(不是重点) 递归的应用场景 编程中 两种解决问题的思维 自下而上(Bottom-Up) 自上而下(Top-
0. 学习目标 递归函数是直接调用自己或通过一系列语句间接调用自己的函数。递归在程序设计有着举足轻重的作用,在很多情况下,借助递归可以优雅的解决问题。本节主要介绍递归的基本概念以及如何构建递归程序。
我有一个问题一直困扰着我,希望有人能提供帮助。我认为它可能必须通过递归和/或排列来解决,但我不是一个足够好的 (PHP) 程序员。 $map[] = array("0", "1", "2", "3")
我有数据 library(dplyr, warn.conflicts = FALSE) mtcars %>% as_tibble() %>% select(mpg, qsec) %>% h
在 q 中,over 的常见插图运算符(operator) /是 implementation of fibonacci sequence 10 {x,sum -2#x}/ 1 1 这确实打印了前 1
我试图理解以下代码片段中的递归调用。 static long fib(int n) { return n <= 1 ? n : fib(n-1) + fib(n-2); } 哪个函数调用首先被
我是一名优秀的程序员,十分优秀!