- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我还在学习 Haskell,我写了以下基数排序函数。它似乎工作正常,但问题是它的内存效率相当低。如果使用 ghc 编译,内存已经超过 500MB,输入列表大小为 10000 个元素。
所以我想问你如何改进以下算法/代码以使其在速度和内存方面更有效。最好的起点是什么?
import System.Random
-- radixsort for positive integers. uses 10 buckets
radixsort :: [Int] -> [Int]
radixsort [] = []
radixsort xs =
-- given the data, get the number of passes that are required for sorting
-- the largest integer
let maxPos = floor ((log (fromIntegral (foldl max 0 xs)) / log 10) + 1)
-- start sorting from digit on position 0 (lowest position) to position 'maxPos'
radixsort' ys pos
| pos < 0 = ys
| otherwise = let sortedYs = radixsort' ys (pos - 1)
newBuckets = radixsort'' sortedYs [[] | i <- [1..10]] pos
in [element | bucket <- newBuckets, element <- bucket]
-- given empty buckets, digit position and list, sort the values into
-- buckets
radixsort'' [] buckets _ = buckets
radixsort'' (y:ys) buckets pos =
let digit = div (mod y (10 ^ (pos + 1))) (10 ^ pos)
(bucketsBegin, bucketsEnd) = splitAt digit buckets
bucket = head bucketsEnd
newBucket = bucket ++ [y]
in radixsort'' ys (bucketsBegin ++ [newBucket] ++ (tail bucketsEnd)) pos
in radixsort' xs maxPos
-- get an random array given an seed
getRandIntArray :: Int -> [Int]
getRandIntArray seed = (randomRs (0, div (maxBound :: Int) 2) (mkStdGen seed))
main = do
value <- (\x -> return x ) (length (radixsort (take 10000 (getRandIntArray 0))))
print value
最佳答案
主要问题是您的功能 radixsort''
, 因为 ++
是 O(n) 并且它每次都复制作为第一个参数给出的列表。
pack (-1) r' _ = r'
pack n r' relems =
let getn = (map snd) . (filter ((n==) . fst))
in pack (n - 1) ((getn relems):r') relems
radixsort'' elems pos =
let digit = \y -> div (mod y (10 ^ (pos + 1))) (10 ^ pos)
relems = zip (map digit elems) elems
in pack 9 [] relems
关于optimization - 在 Haskell 中优化基数排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5277552/
我正在尝试运行以下代码片段,以使曲线适合一些经验数据,但在Julia Optim.jl包中,optimize()方法一直存在问题。我正在使用Julia v1.1.0,并安装了所有正确的软件包。我不断收
时不时你会听到一些故事,这些故事旨在说明某人在某件事上有多擅长,有时你会听到这个人如何热衷于代码优化,以至于他优化了他的延迟循环。 因为这听起来确实是一件奇怪的事情,因为启动“计时器中断”而不是优化的
我正在尝试使用 z3py 作为优化求解器来最大化从一张纸上切出的长方体的体积。 python API 提供了 Optimize() 对象,但使用它似乎不可靠,给我的解决方案显然不准确。 我尝试使用 h
我今天接受了采访。这个问题是为了优化下面的代码。如果我们将在 for 循环之后看到下面的代码,那么下面有四个“if-else”步骤。所以,面试官要求我将其优化为 3 if-else 行。我已经尝试了很
我使用BFGS算法使用Optim.jl库来最小化Julia中的函数。今天,我问了一个关于同一个库的question,但是为了避免混淆,我决定将它分成两部分。 我还想对优化后的负逆黑森州进行估算,以进行
在 haskell 平台中实现许多功能时有一个非常常见的模式让我很困扰,但我找不到解释。这是关于使用嵌套函数进行优化。 where 子句中的嵌套函数旨在进行尾递归的原因对我来说非常清楚(如 lengt
我目前正试图利用 Julia 中的 Optim 包来最小化成本函数。成本函数是 L2 正则化逻辑回归的成本函数。其构造如下; using Optim function regularised_cost
我正在使用 GEKKO 来解决非线性规划问题。我的目标是将 GEKKO 性能与替代方案进行比较,因此我想确保我从 GEKKO 中获得其所能提供的最佳性能。 有n个二元变量,每个变量都分配有一个权
我可以手动更改参数C和epsilon以获得优化结果,但我发现有PSO(或任何其他优化算法)对SVM进行参数优化。没有算法。什么意思:PSO如何自动优化SVM参数?我读了几篇关于这个主题的论文,但我仍然
我正在使用 scipy.optimize.fmin_l_bfgs_b 来解决高斯混合问题。混合分布的均值通过回归建模,其权重必须使用 EM 算法进行优化。 sigma_sp_new, func_val
当你有一个 Option ,编译器知道 NULL永远不是 &T 的可能值, 和 encodes the None variant as NULL instead .这样可以节省空间: use std:
当你有一个 Option ,编译器知道 NULL永远不是 &T 的可能值, 和 encodes the None variant as NULL instead .这样可以节省空间: use std:
以下是说明我的问题的独立示例。 using Optim χI = 3 ψI = 0.5 ϕI(z) = z^-ψI λ = 1.0532733 V0 = 0.8522423425 zE = 0.598
根据MySQL文档关于Optimizing Queries With Explain : * ALL: A full table scan is done for each combination o
我无法预览我的 Google 优化工具体验。 Google 优化抛出以下错误: 最佳答案 我也经常遇到这种情况。 Google 给出的建议是错误的。清除 cookie 并重新启动浏览器并不能解决问题。
我一直在尝试使用 optim()或 optimize()函数来最小化绝对预测误差的总和。 我有 2 个向量,每个长度为 28,1 个包含预测数据,另一个包含过去 28 天的实际数据。 fcst和 ac
在我对各种编译器书籍和网站的独立研究中,我了解到编译器可以优化正在编译的代码的许多不同方法,但我很难弄清楚每种优化会带来多少好处给予。 大多数编译器编写者如何决定首先实现哪些优化?或者哪些优化值得付出
我在我的项目中使用 System.Web.Optimizations BundleConfig。我在我的网站上使用的特定 jQuery 插件遇到了问题。如果我将文件添加到我的 ScriptBundle
我收到这个错误 Error: webpack.optimize.CommonsChunkPlugin has been removed, please use config.optimization.
scipy的optimize.fmin和optimize.leastsq有什么区别?它们似乎在 this example page 中以几乎相同的方式使用.我能看到的唯一区别是 leastsq 实际上
我是一名优秀的程序员,十分优秀!