<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Xhacker&#039;s Base &#187; 编程</title>
	<atom:link href="http://xhacker.shiyiquan.cn/tag/%e7%bc%96%e7%a8%8b/feed/" rel="self" type="application/rss+xml" />
	<link>http://xhacker.shiyiquan.cn</link>
	<description>Since June, 2009.</description>
	<lastBuildDate>Thu, 05 Jan 2012 13:43:05 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
<atom:link rel="hub" href="http://pubsubhubbub.appspot.com"/><atom:link rel="hub" href="http://superfeedr.com/hubbub"/>		<item>
		<title>swap 的速度</title>
		<link>http://xhacker.shiyiquan.cn/swap-speed/</link>
		<comments>http://xhacker.shiyiquan.cn/swap-speed/#comments</comments>
		<pubDate>Wed, 24 Mar 2010 12:01:45 +0000</pubDate>
		<dc:creator>Xhacker</dc:creator>
				<category><![CDATA[太阳不错]]></category>
		<category><![CDATA[OI]]></category>
		<category><![CDATA[swap]]></category>
		<category><![CDATA[编程]]></category>

		<guid isPermaLink="false">http://xhacker.shiyiquan.cn/?p=446</guid>
		<description><![CDATA[今天纠结于 swap 要显式地写出来还是写成函数。写成函数绝对要优美的多。 int main&#40;&#41; &#123; int i; int a = 1, b = 2, tmp; for&#40;i = 1; i &#60;= 100000000; i++&#41; &#123; tmp = a; a = b; b = tmp; &#125; &#160; return 0; &#125; ~~~ 一条分隔线 &#8230; <a href="http://xhacker.shiyiquan.cn/swap-speed/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>今天纠结于 swap 要显式地写出来还是写成函数。写成函数绝对要优美的多。</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">int</span> main<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #993333;">int</span> i<span style="color: #339933;">;</span>
    <span style="color: #993333;">int</span> a <span style="color: #339933;">=</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">,</span> b <span style="color: #339933;">=</span> <span style="color: #0000dd;">2</span><span style="color: #339933;">,</span> tmp<span style="color: #339933;">;</span>
    <span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span>i <span style="color: #339933;">=</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;=</span> <span style="color: #0000dd;">100000000</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        tmp <span style="color: #339933;">=</span> a<span style="color: #339933;">;</span>
        a <span style="color: #339933;">=</span> b<span style="color: #339933;">;</span>
        b <span style="color: #339933;">=</span> tmp<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #b1b100;">return</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>~~~ 一条分隔线 ~~~</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">void</span> swap<span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> <span style="color: #339933;">*</span>a<span style="color: #339933;">,</span> <span style="color: #993333;">int</span> <span style="color: #339933;">*</span>b<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #993333;">int</span> tmp<span style="color: #339933;">;</span>
    tmp <span style="color: #339933;">=</span> <span style="color: #339933;">*</span>a<span style="color: #339933;">;</span>
    <span style="color: #339933;">*</span>a <span style="color: #339933;">=</span> <span style="color: #339933;">*</span>b<span style="color: #339933;">;</span>
    <span style="color: #339933;">*</span>b <span style="color: #339933;">=</span> tmp<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #993333;">int</span> main<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #993333;">int</span> i<span style="color: #339933;">;</span>
    <span style="color: #993333;">int</span> a <span style="color: #339933;">=</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">,</span> b <span style="color: #339933;">=</span> <span style="color: #0000dd;">2</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span>i <span style="color: #339933;">=</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;=</span> <span style="color: #0000dd;">100000000</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        swap<span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span>a<span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>b<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #b1b100;">return</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>显式的 swap 一次要 0.0000000035 秒。<br />
函数的 swap 一次要 0.0000000057 秒。</p>
<p>我还试了一下 C++ 的 inline，发现没有速度的提升。</p>
<p>结论：用函数的 swap。这点速度损失没啥，然而一堆显式的 swap 不仅容易出错还极其影响心情。</p>
]]></content:encoded>
			<wfw:commentRss>http://xhacker.shiyiquan.cn/swap-speed/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
	</channel>
</rss>

