Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 259f4f9a4d
Fetching contributors…

Cannot retrieve contributors at this time

142 lines (132 sloc) 10.319 kb
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="Docutils 0.7: http://docutils.sourceforge.net/" />
<title>抽象和性能 - Stream Fusion</title>
<meta name="viewport" contect="width=device-width"></meta>
<link rel="alternate" type="application/rss+xml" title="blog post summary feeds" href="/blog/rss.xml" />
<link type="text/css" rel="stylesheet" href="css/html4css1.css"/>
<link rel="stylesheet" type="text/css" href="css/blog.css" media="screen" />
<link type="text/css" rel="stylesheet" href="css/pygments.css"/>
<link rel="shortcut icon" href="/favicon.ico" type="image/x-icon"/>
<style type="text/css">
.contents {
margin-top:0;
}
</style>
</head>
<body>
<div id="wrapper">
<div id="header">
<a href="/blog/"><h1>精确编程</h1></a>
<div class="nav-icons">
<p class="nav">
<a title="首页" href="index.html" class=""><img src="images/icons/home.png" alt="首页"/></a>
<a title="文章列表" href="list.html" class=""><img src="images/icons/blog.png" alt="文章列表"/></a>
<a title="订阅" href="rss.xml" class=""><img src="images/icons/about.png" alt="订阅"/></a>
</p>
<p class="link">
<a title="Github" href="https://github.com/yihuang/" class=""><img src="images/icons/github.png" alt="Github"/></a>
<a title="豆瓣" href="http://www.douban.com/people/huangyi/" class=""><img src="images/icons/douban.png" alt="豆瓣"/></a>
<a title="Google Reader Share" href="http://www.google.com/reader/shared/yi.codeplayer" class=""><img src="images/icons/greader.png" alt="Google Reader Share"/></a>
<a title="Delicious" href="http://www.delicious.com/huangyi" class=""><img src="images/icons/delicious.png" alt="Delicious"/></a>
<a title="42区" href="http://42qu.com/-10000020" class=""><img src="images/icons/42qu.png" alt="42区"/></a>
<a title="Blogger" href="http://codeplayer.blogspot.com/" class=""><img src="images/icons/blogger.png" alt="Blogger"/></a>
<a title="豆瓣9点" href="http://9.douban.com/subject/9361637/" class=""><img src="images/icons/douban-nine.png" alt="豆瓣9点"/></a>
<a title="啄木鸟社区" href="http://wiki.woodpecker.org.cn/moin/HuangYi" class=""><img src="images/icons/woodpecker.png" alt="啄木鸟社区"/></a>
</p>
</div>
</div>
<div id="middle">
<div class="postdetail">
<h1 class="title">抽象和性能 - Stream Fusion</h1>
<p class="author">2011-06-27 黄毅</p>
<p>从经验来看,抽象和性能,似乎是鱼和熊掌不能兼得。比如从C到Java到Python,抽象层次越高的语言,性能越低下。</p>
<p>然而当抽象层次再上一个台阶,到达编译型函数式语言,反而能回到C级别的高性能,因为在这里高的抽象层次给编译期代码优化提供了巨大的帮助。</p>
<p>本文介绍一种针对列表处理循环的优化。</p>
<div class="section" id="id1">
<h2>高阶函数和循环</h2>
<p>正如命令式语言的GOTO,递归是函数式语言中很底层的构造,使用它我们可以表达各种循环模式。</p>
<p>函数式语言最崇尚代码复用了,我们可以把常用的循环模式封装成不同的高阶函数,所以实际编程中很多时候只需要跟这些高阶函数打交道。</p>
<p>prim :: [Int]
prim = filter (isPrim l prim) [1..]</p>
<p>filter map zip zipWith fold</p>
<p>循环退出条件</p>
<p>takeWhile f</p>
</div>
<div class="section" id="id2">
<h2>性能分析</h2>
</div>
<div class="section" id="fusion">
<h2>fusion</h2>
<p>map f . map g = map (f . g)</p>
<p>filter f . filter g = filter (x -&gt; f x &amp;&amp; g x)</p>
</div>
<div class="section" id="id3">
<h2>Stream Fusion</h2>
<dl class="docutils">
<dt>data Step s a = Done</dt>
<dd><div class="first last line-block">
<div class="line">Skip !s</div>
<div class="line">Yield !a !s</div>
</div>
</dd>
</dl>
<p>data Stream a = Stream (s -&gt; Step s a) s</p>
<p>stream :: [a] -&gt; Stream a
unstream :: Stream a -&gt; [a]</p>
<p>filter :: (a -&gt; Bool) -&gt; [a] -&gt; [a]</p>
</div>
<br/>
<div id="disqus_thread"></div>
<script type="text/javascript">
var disqus_shortname = 'yi-programmer';
var disqus_identifier = '2011-06-27_abstract-performance';
var disqus_url = 'http://www.yi-programmer.com/blog/2011-06-27_abstract-performance.html';
(function() {
var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
dsq.src = 'http://' + disqus_shortname + '.disqus.com/embed.js';
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
})();
</script>
<noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
<a href="http://disqus.com" class="dsq-brlink">blog comments powered by <span class="logo-disqus">Disqus</span></a>
<p class="announce">转载请注明出处,收藏或分享这篇文章到:</p>
<p class="share-icons">
<a target="_blank" title="分享到豆瓣" href="http://www.douban.com/recommend/?url=http://www.yi-programmer.com/blog/2011-06-27_abstract-performance.html&title=抽象和性能 - Stream Fusion"><img src="images/icons/douban.png" alt="分享到豆瓣"/></a>
<a target="_blank" title="收藏到delicious" href="http://del.icio.us/post?url=http://www.yi-programmer.com/blog/2011-06-27_abstract-performance.html&title=抽象和性能 - Stream Fusion"><img src="images/icons/delicious.png" alt="收藏到delicious"/></a>
<a target="_blank" title="收藏到QQ书签" href="http://shuqian.qq.com/post?from=3&title=抽象和性能 - Stream Fusion&uri=http://www.yi-programmer.com/blog/2011-06-27_abstract-performance.html&jumpback=2&noui=1"><img src="images/icons/qqbookmark.gif" alt="收藏到QQ书签"/></a>
<a target="_blank" title="收藏到google书签" href="https://www.google.com/bookmarks/mark?op=add&bkmk=http://www.yi-programmer.com/blog/2011-06-27_abstract-performance.html&title=抽象和性能 - Stream Fusion"><img src="images/icons/google.gif" alt="收藏到google书签"/></a>
<a target="_blank" title="收藏到百度搜藏" href="http://cang.baidu.com/do/add?iu=http://www.yi-programmer.com/blog/2011-06-27_abstract-performance.html&it=抽象和性能 - Stream Fusion&fr=ien#nw=1"><img src="images/icons/baidu.gif" alt="收藏到百度搜藏"/></a>
<a target="_blank" title="分享到新浪围脖" href="http://v.t.sina.com.cn/share/share.php?url=http://www.yi-programmer.com/blog/2011-06-27_abstract-performance.html&title=抽象和性能 - Stream Fusion&source=bookmark"><img src="images/icons/sina-t.jpg" alt="分享到新浪围脖"/></a>
<a target="_blank" title="分享到facebook" href="http://www.facebook.com/sharer.php?u=http://www.yi-programmer.com/blog/2011-06-27_abstract-performance.html&t=抽象和性能 - Stream Fusion"><img src="images/icons/facebook.png" alt="分享到facebook"/></a>
<a target="_blank" title="分享到人人网" href="http://share.renren.com/share/buttonshare.do?link=http://www.yi-programmer.com/blog/2011-06-27_abstract-performance.html&title=抽象和性能 - Stream Fusion"><img src="images/icons/renren.gif" alt="分享到人人网"/></a>
<a target="_blank" title="分享到开心网" href="http://www.kaixin001.com/repaste/share.php?rtitle=抽象和性能 - Stream Fusion&rurl=http://www.yi-programmer.com/blog/2011-06-27_abstract-performance.html&rcontent=http://www.yi-programmer.com/blog/2011-06-27_abstract-performance.html"><img src="images/icons/kaixin.gif" alt="分享到开心网"/></a>
<a target="_blank" title="分享到QQ空间" href="http://sns.qzone.qq.com/cgi-bin/qzshare/cgi_qzshare_onekey?url=http://www.yi-programmer.com/blog/2011-06-27_abstract-performance.html"><img src="images/icons/qzone.gif" alt="分享到QQ空间"/></a>
<a target="_blank" title="分享到MySpace" href="http://www.myspace.cn/Modules/PostTo/Pages/Default.aspx?u=http://www.yi-programmer.com/blog/2011-06-27_abstract-performance.html&t=抽象和性能 - Stream Fusion"><img src="images/icons/myspace.jpg" alt="分享到MySpace"/></a>
</p>
</div>
<div class="footer">
<hr class="footer" />
<a class="reference external" href="src/2011-06-27_abstract-performance.txt">View document source</a>.
Generated on: 2012-01-31.
Generated by <a class="reference external" href="http://docutils.sourceforge.net/">Docutils</a> from <a class="reference external" href="http://docutils.sourceforge.net/rst.html">reStructuredText</a> source.
</div>
<p class="copyright">Website content copyright &copy; by 黄毅. All rights reserved. </p>
</div>
</div>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript" src="js/main.js"></script>
<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try{
var pageTracker = _gat._getTracker("UA-415070-6");
pageTracker._trackPageview();
} catch(err) {}
</script>
</body>
</html>
Jump to Line
Something went wrong with that request. Please try again.