我对SPOJ上的PRIME1问题的尝试一直很糟糕.我发现使用ByteString 实际上有助于在问题文本中阅读的性能.但是,使用ByteString写出结果实际上比使用Prelude函数稍慢.我想知道我做错了,或者这是否是预期的.
我使用(putStrLn.show)和ByteString等效三种不同的方式进行了分析和计时:
我期望数字2和3执行速度较慢,因为您在一个函数中构建列表并在另一个函数中使用它.通过在生成数字时打印数字,我避免为列表分配任何内存.另一方面,每次调用putStrLn时都要进行一次调用系统调用.对?所以我测试了,#1实际上是最快的.
使用选项#1和Prelude([Char])函数实现了最佳性能.我期望我的最佳表现是ByteString的选项#1,但事实并非如此.我只使用懒惰的ByteStrings,但我认为这不重要.是吗?
一些问题:
我的工作假设是,如果你没有将它们与其他文本组合,用ByteString写出Integer是比较慢的.如果您将Integers与[Char]结合使用,那么使用ByteStrings可以获得更好的性能.即,ByteString重写:
putStrLn $ "the answer is: " ++ (show value)
Run Code Online (Sandbox Code Playgroud)
将比上面写的版本快得多.这是真的?
谢谢阅读!
编译时收到以下错误消息:
重复类型签名:
weightedMedian.hs:71:0-39:findVal :: [ValPair] - > Double - > Double
weightedMedian.hs:68:0-36:findVal :: [ValPair] - > Int - > Double
我的解决方案是找到findValI和findValD.但是,findValI只是将Int类型转换为Double并调用findValD.
另外我不能在Num(Int,Double)类型上进行模式匹配,所以我不能只将类型签名更改为
findVal :: [ValPair] -> Num -> Double
Run Code Online (Sandbox Code Playgroud)
在许多语言中,我不需要不同的名称.为什么我在Haskell中需要不同的名字?这会很难添加到语言中吗?或者那里有龙吗?
Haskell wiki显示您需要设置编译标志和运行时标志以获得多核支持.为什么不使用足够的库来在编译时获得正确的行为?为什么运行时可执行文件检测不到它是使用-threaded编译并使用系统上的所有核心,除非另有说明?我认为默认情况下打开这些会更好.然后可能会有标志关闭或修改这些功能.
http://www.haskell.org/haskellwiki/GHC/Concurrency#Multicore_GHC说:
让标志必须在编译时和运行时再次设置似乎有些繁琐.这些标志是否是为GHC增加并发性的遗留因素?
我正在从服务器上的java程序运行bash脚本.我刚上传了一个新版本的脚本,打算在下一次运行脚本时使用该版本.我并不打算中断现有的,运行的脚本实例.但是,我刚从300台服务器收到100多个崩溃通知.我猜测用新版本替换正在运行的bash脚本导致了这个问题.但是,这将要求运行的bash脚本在进入每个新步骤时从磁盘读取.这是怎么回事?
bash脚本的运行版本运行一些光线跟踪软件.每次运行需要2个小时.子步骤需要5分钟到1.5小时.在脚本中完成一个步骤后,脚本始终报告崩溃.它永远不会报告已经运行的子步骤崩溃.一些崩溃报告没有找到我在脚本中找不到的命令.不同的崩溃报告不同的地方.
救命!
编辑:我使用scp将脚本复制到所有300台服务器.该文件已在文件系统上替换.这不是共享文件.
我正在传递部分应用的功能.完整的签名是:
import Data.Map as Map
-- Update the correct bin of the histogram based on the min value, bin width,
-- the histogram stored as a map, and the actual value we are interested in.
updateHist :: Double -> Double -> Map.Map Bin Double -> Double ->
Map.Map Bin Double
Run Code Online (Sandbox Code Playgroud)
该函数更新存储直方图数据的Map.前两个参数给出了我们感兴趣的数据的下限,下一个是直方图的bin宽度.我在程序启动时填写这些值,并在整个模块中传递部分应用的函数.这意味着我有很多功能,签名如下:
-- Extra the data out of the string and update the histogram (in the Map) with it.
doSomething :: String -> (Map.Map Bin Double -> Double -> Map.Map Bin …
Run Code Online (Sandbox Code Playgroud) 编辑:
事实证明,慢速版本实际上是插入排序O(n ^ 2)而不是解释性能问题的合并排序O(n log n).我想我会为未来的读者节省趟代码来发现这个答案的痛苦.
原来在这里开始-------------------------------------------
我在haskell中编写了两个版本的合并排序,我不能理解为什么这个版本比另一个快1000倍.在这两种情况下,我们首先将列表中的项目排序为创建列表列表的列表.然后我们将列表配对并合并它们,直到只剩下一个列表.问题似乎是我在慢版本中调用"doMerge(x1:x2:xs)= doMerge $ merge x1 x2:doMerge xs"但在快速版本中调用doMerge(mergePairs xs).我对1000x的速度差异感到惊讶!
-- Better version: takes 0.34 seconds to sort a 100,000 integer list.
betMergeSort :: [Int] -> [Int]
betMergeSort list = doMerge $ map (\x -> [x]) list
where
doMerge :: [[Int]] -> [Int]
doMerge [] = []
doMerge [xs] = xs
doMerge xs = doMerge (mergePairs xs)
mergePairs :: [[Int]] -> [[Int]]
mergePairs (x1:x2:xs) = merge x1 x2 : mergePairs xs
mergePairs xs = xs …
Run Code Online (Sandbox Code Playgroud) 尝试编译时出现以下错误
$ ghc --make -O2 -Wall -fforce-recomp
[1/1]编译Main(isPrimeSmart.hs,isPrimeSmart.o)SpecConstr函数`$ wa {v s2we} [lid]'有两种调用模式,但限制为1使用-fspec-constr-count = n来设置绑定使用-dppr-debug查看专业化链接isPrimeSmart ...
我的代码是:
{-# OPTIONS_GHC -O2 -optc-O2 #-}
import qualified Data.ByteString.Lazy.Char8 as StrL -- StrL is STRing Library
import Data.List
-- read in a file. First line tells how many cases. Each case is on a separate
-- line with the lower an upper bounds separated by a space. Print all primes
-- between the lower and upper bound. Separate results for each case with
-- a …
Run Code Online (Sandbox Code Playgroud) 如何将Double转换为Data.Text?
实质上,我有以下代码:
Data.Text.pack $ show 9.0
Run Code Online (Sandbox Code Playgroud)
那段代码有一些相当明显的愚蠢.所以我在文档中挖掘并想出了这个:
toStrict $ toLazyText $ realFloat 9.0
Run Code Online (Sandbox Code Playgroud)
这似乎更好,但似乎应该有一个更直接的方法,但我找不到任何类型Double - > Data.Text.这是最好的方法吗?似乎如果我切换到惰性文本我可以避免这种情况,但我还没有做好准备.
任何智慧的话?
我最近接管了Windows 2003服务器的管理.应用程序日志正在填充以下消息:
Event Type: Failure Audit
Event Source: MSSQLSERVER
Event Category: (4)
Event ID: 18456
Date: 3/5/2010
Time: 4:00:30 PM
User: N/A
Computer: FAIROAKS1
Description:
Login failed for user 'administrator'. [CLIENT: <local machine>]
Data:
0000: 18 48 00 00 0e 00 00 00 .H......
0008: 0a 00 00 00 46 00 41 00 ....F.A.
0010: 49 00 52 00 4f 00 41 00 I.R.O.A.
0018: 4b 00 53 00 31 00 00 00 K.S.1...
0020: 07 00 00 00 6d …
Run Code Online (Sandbox Code Playgroud) 我试图解析一个输入流,其中第一行告诉我有多少行数据.我最终得到以下代码,它可以工作,但我认为还有更好的方法.在那儿?
main = do
numCases <- getLine
proc $ read numCases
proc :: Integer -> IO ()
proc numCases
| numCases == 0 = return ()
| otherwise = do
str <- getLine
putStrLn $ findNextPalin str
proc (numCases - 1)
Run Code Online (Sandbox Code Playgroud)
注意:该代码解决了Sphere问题https://www.spoj.pl/problems/PALIN/,但我认为发布其余代码不会影响对此处操作的讨论.
我有一个haskell程序的问题.我想做这样的事情:
main = do
print $ map foo [(1, [(2, 3), (4,5)])]
foo :: (Int, [(Int, Int)]) -> (Int, [(Int, Int)])
foo (a, [(b, c)]) = (a+1, [(b, c)])
Run Code Online (Sandbox Code Playgroud)
然后我得到运行时错误:
Non-exhaustive patterns in function Main.foo
Run Code Online (Sandbox Code Playgroud)
怎么可能做出这样的行动?我只想访问列表中没有的参数.
命令:
select * from dbo.hmg_cahplots
Run Code Online (Sandbox Code Playgroud)
返回9250行.但是,当我尝试创建一个触发器时,它失败了:
消息8197,级别16,状态6,过程LotUpdateTrigger_tdp,第1行对象'dbo.hmg_cahplots'不存在或对此操作无效.
触发代码是:
CREATE TRIGGER dbo.LotUpdateTrigger_tdp ON dbo.hmg_cahplots FOR UPDATE, INSERT
AS
BEGIN
update lot
set lot.hmg_planmodelname = model.hmg_modelname, lot.hmg_thermslotincentive = model.hmg_thermsincentive,
lot.hmg_thermslotincentive_base = model.hmg_thermsincentive_base, lot.hmg_kwlotincentive = model.hmg_kwincentive
from hmg_cahplots as lot inner join i
on lot.hmg_cahplotsid = i.hmg_cahplotsid
inner join hmg_pgecahp as proj
on proj.hmg_pgecahpid = lot.hmg_pgecahplots
left outer join hmg_pgecahpmodels as model
on model.hmg_pgecahpmodelsid = lot.hmg_cahpplanstolotsid
and model.hmg_pgecahpplansid = lot.hmg_pgecahplots
END
Run Code Online (Sandbox Code Playgroud)
我怀疑这很难解决.我假设我需要指定命名空间或其他东西.但是,我是SQL Server的新手,我不知道如何开始这个.
谢谢 - 蒂姆
haskell ×10
performance ×2
sql-server ×2
bash ×1
bytestring ×1
concurrency ×1
ghc ×1
io ×1
list ×1
multicore ×1
stream ×1
triggers ×1
warnings ×1
xsd ×1