假設(shè)我有一個(gè)整數(shù) x,我要判斷該整數(shù)是否在 [1, limit] 這個(gè)區(qū)間內(nèi),在 Haskell 中我能想到兩種方案:
x >= 1 and x <= limit
elem x [1..limit]
我使用了 GCI 的 Profiling,代碼如下:
main = do
print $ {-# SCC larger_than #-} lth 10000
print $ {-# scc in_range #-} inr 10000
lth :: Integer -> Bool
lth x = (x >= 1 && x <= 65535000000000)
inr :: Integer -> Bool
inr x = let m = 65535000000000::Integer in
x `elem` [1..m]
顯示的結(jié)果:
不知方法對(duì)不對(duì),請(qǐng)問(wèn)兩種方案哪一種更快?
沒(méi)想到SF有Haskell問(wèn)題:)
其實(shí)你看Profiling的結(jié)果,in_range
的inherited %time
和inherited %alloc
都比larger_than
大,占了整個(gè)程序執(zhí)行的100%和93.3%。所以第一種方法快。
寫個(gè)rewrite rule也許可以對(duì)elem
和enumFromTo
進(jìn)行優(yōu)化。
試了寫rewrite rule,發(fā)現(xiàn)enumFromTo有inline,導(dǎo)致rewrite rule沒(méi)有生效,所以先自己實(shí)現(xiàn)一個(gè)myEnumFromTo
看看效果:
module Main where
{-# RULES
"elem/myEnumFromTo" forall (x::Integer) (n::Integer) (m::Integer) . elem x (myEnumFromTo n m) = x >= n && x <= m
#-}
{-# NOINLINE myEnumFromTo #-}
myEnumFromTo n m
| n == m = [n]
| otherwise = [n] ++ myEnumFromTo (n + 1) m
main = do
print $ {-# SCC larger_than #-} lth 100000000
print $ {-# scc in_range #-} inr 100000000
lth :: Integer -> Bool
lth x = (x >= 1 && x <= 65535000000000)
inr :: Integer -> Bool
inr x = let m = 65535000000000::Integer in
x `elem` (myEnumFromTo 1 m)
Profile結(jié)果是這樣的:
main Main app/Main.hs:(12,1)-(14,48) 0.0 15.8
individual inherited
COST CENTRE MODULE SRC no. entries %time %alloc %time %alloc
MAIN MAIN <built-in> 144 0 0.0 29.6 0.0 100.0
CAF GHC.Conc.Signal <entire-module> 252 0 0.0 0.9 0.0 0.9
CAF GHC.IO.Encoding <entire-module> 241 0 0.0 3.8 0.0 3.8
CAF GHC.IO.Encoding.Iconv <entire-module> 239 0 0.0 0.3 0.0 0.3
CAF GHC.IO.Handle.FD <entire-module> 231 0 0.0 47.3 0.0 47.3
CAF GHC.IO.Handle.Text <entire-module> 229 0 0.0 0.1 0.0 0.1
CAF GHC.Show <entire-module> 214 0 0.0 0.4 0.0 0.4
CAF GHC.Event.Thread <entire-module> 193 0 0.0 1.7 0.0 1.7
CAF GHC.Event.Poll <entire-module> 160 0 0.0 0.1 0.0 0.1
CAF:main1 Main <no location info> 286 0 0.0 0.0 0.0 0.0
main Main app/Main.hs:(12,1)-(14,48) 288 1 0.0 0.0 0.0 0.0
CAF:main2 Main <no location info> 284 0 0.0 0.0 0.0 0.0
main Main app/Main.hs:(12,1)-(14,48) 293 0 0.0 0.0 0.0 0.0
in_range Main app/Main.hs:14:32-48 294 1 0.0 0.0 0.0 0.0
inr Main app/Main.hs:(20,1)-(21,29) 295 1 0.0 0.0 0.0 0.0
inr.m Main app/Main.hs:20:13-39 296 1 0.0 0.0 0.0 0.0
CAF:main4 Main <no location info> 285 0 0.0 0.0 0.0 0.0
main Main app/Main.hs:(12,1)-(14,48) 290 0 0.0 0.0 0.0 0.0
larger_than Main app/Main.hs:13:36-48 291 1 0.0 0.0 0.0 0.0
lth Main app/Main.hs:17:1-39 292 1 0.0 0.0 0.0 0.0
main Main app/Main.hs:(12,1)-(14,48) 289 0 0.0 15.8 0.0 15.8
還不知道怎讓才能對(duì)enumFromTo
生效:(
北大青鳥APTECH成立于1999年。依托北京大學(xué)優(yōu)質(zhì)雄厚的教育資源和背景,秉承“教育改變生活”的發(fā)展理念,致力于培養(yǎng)中國(guó)IT技能型緊缺人才,是大數(shù)據(jù)專業(yè)的國(guó)家
北大青鳥中博軟件學(xué)院創(chuàng)立于2003年,作為華東區(qū)著名互聯(lián)網(wǎng)學(xué)院和江蘇省首批服務(wù)外包人才培訓(xùn)基地,中博成功培育了近30000名軟件工程師走向高薪崗位,合作企業(yè)超4
中公教育集團(tuán)創(chuàng)建于1999年,經(jīng)過(guò)二十年潛心發(fā)展,已由一家北大畢業(yè)生自主創(chuàng)業(yè)的信息技術(shù)與教育服務(wù)機(jī)構(gòu),發(fā)展為教育服務(wù)業(yè)的綜合性企業(yè)集團(tuán),成為集合面授教學(xué)培訓(xùn)、網(wǎng)
達(dá)內(nèi)教育集團(tuán)成立于2002年,是一家由留學(xué)海歸創(chuàng)辦的高端職業(yè)教育培訓(xùn)機(jī)構(gòu),是中國(guó)一站式人才培養(yǎng)平臺(tái)、一站式人才輸送平臺(tái)。2014年4月3日在美國(guó)成功上市,融資1
曾工作于聯(lián)想擔(dān)任系統(tǒng)開發(fā)工程師,曾在博彥科技股份有限公司擔(dān)任項(xiàng)目經(jīng)理從事移動(dòng)互聯(lián)網(wǎng)管理及研發(fā)工作,曾創(chuàng)辦藍(lán)懿科技有限責(zé)任公司從事總經(jīng)理職務(wù)負(fù)責(zé)iOS教學(xué)及管理工作。
浪潮集團(tuán)項(xiàng)目經(jīng)理。精通Java與.NET 技術(shù), 熟練的跨平臺(tái)面向?qū)ο箝_發(fā)經(jīng)驗(yàn),技術(shù)功底深厚。 授課風(fēng)格 授課風(fēng)格清新自然、條理清晰、主次分明、重點(diǎn)難點(diǎn)突出、引人入勝。
精通HTML5和CSS3;Javascript及主流js庫(kù),具有快速界面開發(fā)的能力,對(duì)瀏覽器兼容性、前端性能優(yōu)化等有深入理解。精通網(wǎng)頁(yè)制作和網(wǎng)頁(yè)游戲開發(fā)。
具有10 年的Java 企業(yè)應(yīng)用開發(fā)經(jīng)驗(yàn)。曾經(jīng)歷任德國(guó)Software AG 技術(shù)顧問(wèn),美國(guó)Dachieve 系統(tǒng)架構(gòu)師,美國(guó)AngelEngineers Inc. 系統(tǒng)架構(gòu)師。