<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:blogger='http://schemas.google.com/blogger/2008' xmlns:georss='http://www.georss.org/georss' xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-8742529245277406456</id><updated>2024-12-04T11:04:40.552-08:00</updated><category term="Programming"/><category term="Network"/><category term="Insterests"/><category term="All about Apple"/><category term="Interests"/><category term="Miscellaneous"/><category term="OS"/><category term="Thoughts"/><category term="System Programming"/><category term="Computational Geometry"/><category term="Geometry"/><category term="Java"/><category term="SQL"/><category term="Linux"/><category term="Mathimatical modeling"/><category term="Perl"/><category term="Python"/><category term="Security"/><category term="Writing Skills"/><category term="Hardware"/><category term="Presentation Skills"/><category term="Qt"/><category term="STL study"/><category term="WSNs"/><category term="Ethics Issues"/><category term="Interview"/><category term="Kinect"/><category term="Probability"/><category term="Social Networking"/><title type='text'>My Little Notebook</title><subtitle type='html'>Pursuing my dream, proceed sloooowly but stably... :-)</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://sophie-notes.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8742529245277406456/posts/default?redirect=false'/><link rel='alternate' type='text/html' href='http://sophie-notes.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><link rel='next' type='application/atom+xml' href='http://www.blogger.com/feeds/8742529245277406456/posts/default?start-index=26&amp;max-results=25&amp;redirect=false'/><author><name>Sophie</name><uri>http://www.blogger.com/profile/08130938292479634191</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgJYm9W1Baj1dfsHW3J9uikPP0eW2P-JuM4uRrJUZI8b-V2_H1C4tGNKzw_wTUUDzxo6VN9r7LKCHoG4ApDuPcc_X2oqejeV_pL9J1K5ezSnXluTppUj60ZBQQBjt73dA/s220/12.jpg'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>96</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-8742529245277406456.post-3095745590030050191</id><published>2013-04-21T16:51:00.000-07:00</published><updated>2013-04-27T14:34:16.346-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Interests"/><category scheme="http://www.blogger.com/atom/ns#" term="Security"/><title type='text'>Digital Signature</title><content type='html'>A digital signature is a mathematical scheme for demonstrating the authenticity of a message. It provides&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;http://en.wikipedia.org/wiki/Authentication&quot; target=&quot;_blank&quot;&gt;authentication&lt;/a&gt;: the message was created by a known sender&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://en.wikipedia.org/wiki/Non-repudiation&quot; target=&quot;_blank&quot;&gt;non-repudiation&lt;/a&gt;: the sender cannot deny having sent the message&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://en.wikipedia.org/wiki/Integrity&quot; target=&quot;_blank&quot;&gt;integrity&lt;/a&gt;: the message was not altered during transferring.&lt;/li&gt;
&lt;/ul&gt;
It uses &lt;a href=&quot;http://en.wikipedia.org/wiki/Asymmetric_key_algorithm&quot; target=&quot;_blank&quot;&gt;asymmetric cryptography&lt;/a&gt; such that sender signs a message with his private key while receiver verify the message with a corresponding public key.&lt;br /&gt;
&lt;br /&gt;
In previous &lt;a href=&quot;http://sophie-notes.blogspot.com/2013/04/md5-vs-shax-and-cryptographic-hash.html&quot; target=&quot;_blank&quot;&gt;post&lt;/a&gt;, we discussed cryptographic hash algorithms and compared several popular ones. &lt;a href=&quot;http://en.wikipedia.org/wiki/Digital_signature&quot; target=&quot;_blank&quot;&gt;Digital signature&lt;/a&gt; usually work together with cryptographic hash algorithms but the two are totally different. The major difference is Hash algorithm does not require any secret key while digital signing algorithm does.&lt;br /&gt;
&lt;br /&gt;
A digital signature consists of three algorithms:&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;&lt;b&gt;Key generation algorithm&lt;/b&gt; that given a set of possible private keys, produces a private key and its corresponding public key.&lt;/li&gt;
&lt;li&gt;&lt;b&gt;Signing algorithm&lt;/b&gt; that given a message and a private key, produces a signature.&lt;/li&gt;
&lt;li&gt;&lt;b&gt;Signature verification algorithm&lt;/b&gt; that given a message, public key and a signature, verify the authenticity of the message.&lt;/li&gt;
&lt;/ul&gt;
&lt;div&gt;
To sign a message, the signer use some cryptographic hash algorithm to generate a digest of the message, and encrypt the digest using the private key.&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
To verify a message, the receiver use the same cryptographic hash algorithm to generate the digest, and then decrypt the signature using the corresponding public key and verify it is equal to the digest.&lt;br /&gt;
&lt;br /&gt;
A typical process is shown in the diagram&lt;span style=&quot;font-size: xx-small;&quot;&gt;[1]&lt;/span&gt; below.&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi0i1PXfQg8Xg26HVCY2fWc6UskbbEv7zHtY2Xrp5e1zz908HLemEiu3Dz-PQPHTqlb95iqm8Lm6JVKuyYJW-sEchHO_xdVZOodMlP44BdeHtmZYEKZYl1OGrnI-rUdXV9gJmRMyx0f7xM/s1600/Basic+RSA+Digital+Signature+Process.gif&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi0i1PXfQg8Xg26HVCY2fWc6UskbbEv7zHtY2Xrp5e1zz908HLemEiu3Dz-PQPHTqlb95iqm8Lm6JVKuyYJW-sEchHO_xdVZOodMlP44BdeHtmZYEKZYl1OGrnI-rUdXV9gJmRMyx0f7xM/s1600/Basic+RSA+Digital+Signature+Process.gif&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
_____________&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;i&gt;Reference:&lt;/i&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;i&gt;[1] &amp;nbsp;&lt;a href=&quot;http://technet.microsoft.com/en-us/library/cc962021.aspx&quot; target=&quot;_blank&quot;&gt;Digital Signatures from Microsoft TechNet&lt;/a&gt;&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://sophie-notes.blogspot.com/feeds/3095745590030050191/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/8742529245277406456/3095745590030050191' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8742529245277406456/posts/default/3095745590030050191'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8742529245277406456/posts/default/3095745590030050191'/><link rel='alternate' type='text/html' href='http://sophie-notes.blogspot.com/2013/04/digital-signature.html' title='Digital Signature'/><author><name>Sophie</name><uri>http://www.blogger.com/profile/08130938292479634191</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgJYm9W1Baj1dfsHW3J9uikPP0eW2P-JuM4uRrJUZI8b-V2_H1C4tGNKzw_wTUUDzxo6VN9r7LKCHoG4ApDuPcc_X2oqejeV_pL9J1K5ezSnXluTppUj60ZBQQBjt73dA/s220/12.jpg'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi0i1PXfQg8Xg26HVCY2fWc6UskbbEv7zHtY2Xrp5e1zz908HLemEiu3Dz-PQPHTqlb95iqm8Lm6JVKuyYJW-sEchHO_xdVZOodMlP44BdeHtmZYEKZYl1OGrnI-rUdXV9gJmRMyx0f7xM/s72-c/Basic+RSA+Digital+Signature+Process.gif" height="72" width="72"/><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8742529245277406456.post-678707091609236651</id><published>2013-04-21T15:24:00.001-07:00</published><updated>2013-04-21T15:35:10.318-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Interests"/><category scheme="http://www.blogger.com/atom/ns#" term="Security"/><title type='text'>MD5 vs. SHAx and Cryptographic Hash Algorithms</title><content type='html'>&lt;h3&gt;
&lt;a href=&quot;http://en.wikipedia.org/wiki/Cryptographic_hash_function&quot; target=&quot;_blank&quot;&gt;Cryptographic Hash Algorithms&lt;/a&gt;&lt;/h3&gt;
A cryptographic hash algorithm takes a string of &lt;i&gt;any size &lt;/i&gt;as an input and output a &lt;i&gt;fixed-size &lt;/i&gt;hash value. The size of outputs vary on different algorithms. Each hash algorithm is fully defined and consistent. That said,&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;No secret key is involved in the algorithm.&lt;/li&gt;
&lt;li&gt;A given hash function should always return the same hash result for a same input.&lt;/li&gt;
&lt;/ul&gt;
A well-designed hash algorithm should at least satisfy the following three properties:&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;Collision&amp;nbsp;Resistance&lt;br /&gt;
Is it &lt;b&gt;computationally feasible&lt;/b&gt; to find any two different messages that have a same hash result (such a pair is called a &lt;i&gt;collision&lt;/i&gt;)?&lt;/li&gt;
&lt;li&gt;Pre-image Resistance&lt;br /&gt;
Given a hash value, is it &lt;b&gt;computationally feasible&lt;/b&gt; to find any message that hash to that value?&lt;/li&gt;
&lt;li&gt;Second Pre-image Resistance&lt;br /&gt;
Given a message, is it &lt;b&gt;computationally feasible&lt;/b&gt; to find another different message that has the same hash result?&lt;/li&gt;
&lt;/ul&gt;
&lt;h4&gt;
&lt;a href=&quot;http://en.wikipedia.org/wiki/Collision_resistance&quot; target=&quot;_blank&quot;&gt;Collision Resistance&lt;/a&gt;&lt;/h4&gt;
An important usage of cryptographic hash algorithm is to check the integrity of a message, which could be a file, a password, or some data.&lt;br /&gt;
In such situations, cryptographic hash algorithms are offen used together with a&amp;nbsp;&lt;a href=&quot;http://en.wikipedia.org/wiki/Digital_signature&quot; target=&quot;_blank&quot;&gt;Digital Signature&lt;/a&gt;&amp;nbsp;algorithm since digital signing algorithms usually work on limited size of input. So, typically, people use a&amp;nbsp;cryptographic&amp;nbsp;hash algorithm to produce a fixed-size of &quot;digest&quot; of the original message and then run a signing algorithm on the hash result to produce the digital signature for the original message.&lt;br /&gt;
&lt;br /&gt;
This type of applications require that the hash algorithm has to be collision resistant.&lt;br /&gt;
&lt;br /&gt;
For example, suppose Bob wants to trick Alice with a fraudulent message and Alice only trust messages that signed by Joe.&lt;br /&gt;
If the hash algorithm is not collision resistant, Bob can create a correct message and a fraudulent one, generate a huge number of variations on the two messages by inserting commas/empty lines/white spaces etc. (such that the contents are not altered), apply the hash algorithm on the two sets until he finds a pair of messages that hash same.&lt;br /&gt;
He then send the correct message to Joe to sign the message but send the fraudulent message with Joe&#39;s signature to Alice.&lt;br /&gt;
Such malicious&amp;nbsp;behavior&amp;nbsp;is known as a &lt;a href=&quot;http://en.wikipedia.org/wiki/Birthday_attack&quot; target=&quot;_blank&quot;&gt;birthday attack&lt;/a&gt;.&lt;br /&gt;
&lt;br /&gt;
Notice that we were saying &quot;is it computationally feasible&quot; rather than &quot;is it possible&quot;. According to &lt;a href=&quot;http://en.wikipedia.org/wiki/Pigeonhole_principle&quot; target=&quot;_blank&quot;&gt;Pigeonhole principle&lt;/a&gt;, there must exist collisions for any hash algorithm since we are mapping infinite set of input to finite set of output. That said, ALL hash algorithms are broken somehow. But if a collision of a hash algorithm can only be computed through a &lt;a href=&quot;http://en.wikipedia.org/wiki/Brute_force_attack&quot; target=&quot;_blank&quot;&gt;brute force attack&lt;/a&gt;, which has time complexity of 2^(n/2) where n is the output size, the hash algorithm can be referred to as strong collision resistant.&lt;br /&gt;
&lt;h4&gt;
&lt;a href=&quot;http://en.wikipedia.org/wiki/Preimage_attack&quot; target=&quot;_blank&quot;&gt;Pre-image Resistance&lt;/a&gt;&lt;/h4&gt;
There are two types of pre-image attacks. Collision resistance implies 2nd pre-image resistance but it doesn&#39;t guarantee pre-image resistance.&lt;br /&gt;
&lt;br /&gt;
Back to the previous example.&lt;br /&gt;
For example, suppose Bob wants to trick Alice with a fraudulent message and Alice only trust messages that signed by Joe.&lt;br /&gt;
If the hash algorithm is not pre-image resistant and suppose Bob overhear a signature for a message from Joe, Bob can generate a huge number of variations on the fraudulent message by inserting commas/empty lines/white spaces etc., apply the hash algorithm on the set until he finds a fraudulent message that hash same. He then send the fraudulent message with Joe&#39;s signature to Alice.&lt;br /&gt;
&lt;br /&gt;
If the hash algorithm is not 2nd pre-image resistant and suppose Bob overhear a message from Joe, again, Bob can generate a huge number of variations on the fraudulent message and apply the hash algorithm on the set until he finds one that hash same. He then send the fraudulent message with Joe&#39;s signature to Alice.&lt;br /&gt;
Such malicious behaviors are typical in &lt;a href=&quot;http://en.wikipedia.org/wiki/Man-in-the-middle_attack&quot; target=&quot;_blank&quot;&gt;man-in-the-middle attack&lt;/a&gt;.&lt;br /&gt;
&lt;br /&gt;
In general, a collision attach is easier than a pre-image attack. A brute force attack for pre-image resistance has time complexity of 2^n (&lt;i&gt;c.f. 2^(n/2) for collision resistance&lt;/i&gt;).&lt;br /&gt;
&lt;h3&gt;
MD5 vs. SHAx&lt;/h3&gt;
A full comparison table can be found on &lt;a href=&quot;http://en.wikipedia.org/wiki/Cryptographic_hash_function#Cryptographic_hash_algorithms&quot; target=&quot;_blank&quot;&gt;wiki&lt;/a&gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;table border=&quot;1&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;Algorithm&lt;/td&gt; &lt;td&gt;Output Size (bits)&lt;/td&gt; &lt;td&gt;Hexadecimal Size&lt;/td&gt; &lt;td&gt;Collision&lt;/td&gt; &lt;td&gt;Pre-image&lt;/td&gt; &lt;td&gt;Second Pre-image&lt;/td&gt; &lt;/tr&gt;
&lt;tr&gt; &lt;td&gt;&lt;a href=&quot;http://en.wikipedia.org/wiki/MD5&quot;&gt;MD5&lt;/a&gt;&lt;/td&gt; &lt;td&gt;128&lt;/td&gt; &lt;td&gt;32&lt;/td&gt; &lt;td&gt;Broken&lt;/td&gt; &lt;td&gt;Broken&lt;/td&gt; &lt;td&gt;Resistant&lt;/td&gt; &lt;/tr&gt;
&lt;tr&gt; &lt;td&gt;&lt;a href=&quot;http://en.wikipedia.org/wiki/SHA-1&quot;&gt;SHA1&lt;/a&gt;&lt;/td&gt; &lt;td&gt;160&lt;/td&gt; &lt;td&gt;40&lt;/td&gt; &lt;td&gt;Broken&lt;/td&gt; &lt;td&gt;Resistant&lt;/td&gt; &lt;td&gt;Resistant&lt;/td&gt; &lt;/tr&gt;
&lt;tr&gt; &lt;td&gt;&lt;a href=&quot;http://en.wikipedia.org/wiki/SHA-2&quot;&gt;SHA2&lt;/a&gt;&lt;/td&gt; &lt;td&gt;224/256/384/512&lt;/td&gt; &lt;td&gt;56/64/96/128&lt;/td&gt; &lt;td&gt;Weak&lt;/td&gt; &lt;td&gt;Weak&lt;/td&gt; &lt;td&gt;Resistant&lt;/td&gt; &lt;/tr&gt;
&lt;tr&gt; &lt;td&gt;&lt;a href=&quot;http://en.wikipedia.org/wiki/SHA-2&quot;&gt;SHA512&lt;/a&gt;&lt;/td&gt; &lt;td&gt;512&lt;/td&gt; &lt;td&gt;128&lt;/td&gt; &lt;td&gt;Weak&lt;/td&gt; &lt;td&gt;Weak&lt;/td&gt; &lt;td&gt;Resistant&lt;/td&gt; &lt;/tr&gt;
&lt;tr&gt; &lt;td&gt;&lt;a href=&quot;http://en.wikipedia.org/wiki/SHA-3&quot;&gt;SHA3&lt;/a&gt;&lt;/td&gt; &lt;td&gt;224/256/384/512&lt;/td&gt; &lt;td&gt;56/64/96/128&lt;/td&gt; &lt;td&gt;Resistant&lt;/td&gt; &lt;td&gt;Resistant&lt;/td&gt; &lt;td&gt;Resistant&lt;/td&gt; &lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;br /&gt;
In a short, SHA2 is recommended for now. (But it will be replaced by SHA3 in a few yeats. :)&lt;br /&gt;
&lt;br /&gt;
A collision of MD5 can be found in a few seconds on your laptop. So it is pretty broken. But it doesn&#39;t mean you cannot use it in any cases. If you don&#39;t care about birthday attack and pre-image attack, MD5 algorithm is good in terms of speed and size. (Well, I suspect in most cases fast may not be a good thing for cryptographic algorithm because it also means that attacker can perform brute-force attach fast.)&lt;br /&gt;
&lt;br /&gt;
SHA1 was popular but has been approved to be broken by researchers. You can use SHA1 in cases where birthday attack is not gonna happen.&lt;br /&gt;
&lt;br /&gt;
Reduced-round SHA2 has also be approved to be broken. It looks likely the full round SHA2 will be broken in a few years.&lt;br /&gt;
___________________________________&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;i&gt;References:&lt;/i&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;i&gt;- Cryptographic hash function on &lt;a href=&quot;http://en.wikipedia.org/wiki/Cryptographic_hash_function&quot; target=&quot;_blank&quot;&gt;wiki&lt;/a&gt;&lt;/i&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;i&gt;- Discussions on stackoverflow [&lt;a href=&quot;http://stackoverflow.com/questions/800685/which-cryptographic-hash-function-should-i-choose&quot; target=&quot;_blank&quot;&gt;1&lt;/a&gt;] [&lt;a href=&quot;http://stackoverflow.com/questions/157998/whats-the-difference-between-sha-and-md5-in-php&quot; target=&quot;_blank&quot;&gt;2&lt;/a&gt;]&lt;/i&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;i&gt;- Discussion on stackexchange [&lt;a href=&quot;http://security.stackexchange.com/questions/2298/what-are-the-differences-between-md5-sha-and-rsa&quot; target=&quot;_blank&quot;&gt;1&lt;/a&gt;]&lt;/i&gt;&lt;/span&gt;</content><link rel='replies' type='application/atom+xml' href='http://sophie-notes.blogspot.com/feeds/678707091609236651/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/8742529245277406456/678707091609236651' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8742529245277406456/posts/default/678707091609236651'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8742529245277406456/posts/default/678707091609236651'/><link rel='alternate' type='text/html' href='http://sophie-notes.blogspot.com/2013/04/md5-vs-shax-and-cryptographic-hash.html' title='MD5 vs. SHAx and Cryptographic Hash Algorithms'/><author><name>Sophie</name><uri>http://www.blogger.com/profile/08130938292479634191</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgJYm9W1Baj1dfsHW3J9uikPP0eW2P-JuM4uRrJUZI8b-V2_H1C4tGNKzw_wTUUDzxo6VN9r7LKCHoG4ApDuPcc_X2oqejeV_pL9J1K5ezSnXluTppUj60ZBQQBjt73dA/s220/12.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8742529245277406456.post-6114993634591941969</id><published>2013-03-23T18:36:00.002-07:00</published><updated>2013-03-23T18:37:13.200-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Miscellaneous"/><category scheme="http://www.blogger.com/atom/ns#" term="OS"/><category scheme="http://www.blogger.com/atom/ns#" term="Perl"/><category scheme="http://www.blogger.com/atom/ns#" term="Security"/><category scheme="http://www.blogger.com/atom/ns#" term="System Programming"/><title type='text'>SSL Validation Notes: LWP::UserAgent (5.8 vs. 6.0) and Openssl (0.9.8 vs. 1.0.0)</title><content type='html'>&lt;h3&gt;
LWP::UserAgent (5.8 vs. 6.0)&lt;/h3&gt;
&lt;div&gt;
If you want to use LWP to turn on SSL validations, i.e. verify that the&amp;nbsp;certificate&amp;nbsp;(cert name, expiration date, and CA chain, etc.) of the connecting server is signed by a trusted authority and issued to the correct host, be careful! You need to make sure you are using the correct perl library.&lt;/div&gt;
&lt;div&gt;
&lt;h4&gt;
5.837 and earlier&lt;/h4&gt;
&lt;/div&gt;
&lt;div&gt;
For &lt;code&gt;libwww-perl-5.837&lt;/code&gt; and earlier releases, LWP will not verify the peer name and thus can&#39;t ensure the connecting server has a valid certificate that matches the expected hostname. That said, it will not detect impersonate attack!&lt;br /&gt;
&lt;br /&gt;
If you are using &lt;code&gt;libwww-perl-6.0.1&lt;/code&gt; and &lt;code&gt;IO-Socket-SSL-1.38&lt;/code&gt; and above, you may be able to turn on the peer check via&lt;br /&gt;
&lt;blockquote class=&quot;tr_bq&quot;&gt;
&lt;code&gt;IO::Socket::SSL::set_ctx_defaults(&lt;br /&gt;
&amp;nbsp; SSL_verify_mode =&amp;gt; Net::SSLeay-&amp;gt;VERIFY_PEER(),&lt;br /&gt;
&amp;nbsp; SSL_verifycn_scheme =&amp;gt; &#39;http&#39; )&lt;/code&gt;&lt;/blockquote&gt;
Otherwise, you have to upgrade to 6.0.1 or later to ensure SSL certificates are properly validated... :-(&lt;/div&gt;
&lt;div&gt;
&lt;h4&gt;
6.0.1 and later&lt;/h4&gt;
Since &lt;code&gt;libwww-perl-6.0.1&lt;/code&gt;, LWP, for secure protocol schemes, by default performs a check to ensure that you are communicating with the expected peer. This check can be turned on/off use&lt;/div&gt;
&lt;blockquote class=&quot;tr_bq&quot;&gt;
&lt;code&gt;ssl_opts ( verify_hostname =&amp;gt; 0 or 1 )&lt;/code&gt;&lt;/blockquote&gt;
Note that &lt;code&gt;ssl_opts&lt;/code&gt; is also first introduced in &lt;code&gt;libwww-perl-6.0.1&lt;/code&gt;.&lt;br /&gt;
&lt;h3&gt;
Openssl (0.9.8 vs. 1.0.0)&lt;/h3&gt;
&lt;div&gt;
When moving from 0.9.8n to 1.0.0, OpenSSL&lt;br /&gt;
&lt;blockquote class=&quot;tr_bq&quot;&gt;
&quot;&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;Enhance the hash format used for certificate directory links. The new form uses the canonical encoding (meaning equivalent names will work even if they aren&#39;t identical) and uses SHA1 instead of MD5.&lt;/span&gt;&quot; (-- from CHANGE log)&lt;/blockquote&gt;
&lt;/div&gt;
&lt;div&gt;
That said, if you use the &lt;code&gt;c_rehash&lt;/code&gt; in OpenSSL 1.0.0 to rebuild symbolic links of your CApath directory, the resulting symlinks will not work with OpenSSL 0.9.8; vice versa, symlinks generated by OpenSSL 0.9.8 &lt;code&gt;c_rehash&lt;/code&gt; will not be recognized by software compiled with OpenSSL 1.0.0.&lt;/div&gt;
&lt;br /&gt;
Although x509 in OpenSSL 1.0.0 added a new &lt;code&gt;-subject_hash_old&lt;/code&gt; and &lt;code&gt;-issuer_hash_old&lt;/code&gt; options for back compatibility, &lt;code&gt;c_rehash&lt;/code&gt; didn&#39;t and doesn&#39;t. This could be a pain point when migrating from 0.9.8 to 1.0.0...&lt;br /&gt;
&lt;br /&gt;
So, if you are using some software compiled with OpenSSL 0.9.8 to build symlinks but then deploy the certs and their symlinks to multiple OSRev platforms, where some of them use OpenSSL 1.0.0, you need to upgrade the software to OpenSSL 1.0.0. But then it will not work for older platforms running OpenSSL 0.9.8. So, you also need to patch your c_rehash so that it could build/generate two copies of symlinks for each certificate, one for new version and one for old one.&lt;br /&gt;
&lt;ol&gt;&lt;/ol&gt;
First, add an option for hash algorithm in &lt;code&gt;link_hash_cert&lt;/code&gt; in &lt;code&gt;c_rehash&lt;/code&gt;. Let &lt;code&gt;link_hash_cert&lt;/code&gt; use the new algorithm by default. Note that &lt;code&gt;-hash&lt;/code&gt; is a synonym for &lt;code&gt;-subject_hash&lt;/code&gt; (see &lt;a href=&quot;https://www.openssl.org/docs/apps/x509.html&quot; target=&quot;_blank&quot;&gt;x509 document&lt;/a&gt;).&lt;br /&gt;
&lt;pre style=&quot;background-image: URL(https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiqsl8vm2ODGqxjnZwd7bqtBugZa1EOe4r3nFGP9ErfkJRPdYStTvkuoNm_8pXw-W_126q5s1dWXJ93It7AszOaCgpKagDLlNQC__kF7lWOw6TV-wMnL0S0DLhwKUhkYPqDKAJc4DTKu29C/s320/codebg.gif); background: #f0f0f0; border: 1px dashed #CCCCCC; color: black; font-family: arial; font-size: 12px; height: 200px; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;&quot;&gt;&lt;code style=&quot;color: black; word-wrap: normal;&quot;&gt; sub link_hash_cert {  
     my $fname = $_[0];  
+    my $hashalgo = $_[1] || &#39;-subject_hash&#39;;  
     $fname =~ s/&#39;/&#39;\\&#39;&#39;/g;  
-    my ($hash, $fprint) = `&quot;$openssl&quot; x509 -hash -fingerprint -noout -in &#39;$fname&#39;`;  
+    my ($hash, $fprint) = `&quot;$openssl&quot; x509 $hashalgo -fingerprint -noout -in &#39;$fname&#39;`;  
     chomp $hash;  
     chomp $fprint;  
     $fprint =~ s/^.*=//;  
     $fprint =~ tr/://d;  
     my $suffix = 0;  
     # Search for an unused hash filename  
     while(exists $hashlist{&quot;$hash.$suffix&quot;}) {  
         # Hash matches: if fingerprint matches its a duplicate cert  
         if($hashlist{&quot;$hash.$suffix&quot;} eq $fprint) {  
             print STDERR &quot;WARNING: Skipping duplicate certificate $fname\n&quot;;  
             return;  
         }  
         $suffix++;  
     }  
     $hash .= &quot;.$suffix&quot;;  
     print &quot;$fname =&amp;gt; $hash\n&quot;;  
     $symlink_exists=eval {symlink(&quot;&quot;,&quot;&quot;); 1};  
     if ($symlink_exists) {  
         symlink $fname, $hash;  
     } else {  
         system (&quot;cp&quot;, $fname, $hash);  
     }  
     $hashlist{$hash} = $fprint;  
 }  
&lt;/code&gt;&lt;/pre&gt;
Then add another function that call &lt;code&gt;link_hash_cert&lt;/code&gt; but using old hash algorithm.&lt;br /&gt;
&lt;pre style=&quot;background-image: URL(https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiqsl8vm2ODGqxjnZwd7bqtBugZa1EOe4r3nFGP9ErfkJRPdYStTvkuoNm_8pXw-W_126q5s1dWXJ93It7AszOaCgpKagDLlNQC__kF7lWOw6TV-wMnL0S0DLhwKUhkYPqDKAJc4DTKu29C/s320/codebg.gif); background: #f0f0f0; border: 1px dashed #CCCCCC; color: black; font-family: arial; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;&quot;&gt;&lt;code style=&quot;color: black; word-wrap: normal;&quot;&gt;+ sub link_hash_cert_compat {  
+     link_hash_cert($_[0], &#39;-subject_hash_old&#39;);  
+ }  
&lt;/code&gt;&lt;/pre&gt;
Finally, let &lt;code&gt;hash_dir&lt;/code&gt; call both functions if &lt;code&gt;$cert&lt;/code&gt; presents.&lt;br /&gt;
&lt;pre style=&quot;background-image: URL(https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiqsl8vm2ODGqxjnZwd7bqtBugZa1EOe4r3nFGP9ErfkJRPdYStTvkuoNm_8pXw-W_126q5s1dWXJ93It7AszOaCgpKagDLlNQC__kF7lWOw6TV-wMnL0S0DLhwKUhkYPqDKAJc4DTKu29C/s320/codebg.gif); background: #f0f0f0; border: 1px dashed #CCCCCC; color: black; font-family: arial; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;&quot;&gt;&lt;code style=&quot;color: black; word-wrap: normal;&quot;&gt; sub hash_dir {  
     my %hashlist;  
     print &quot;Doing $_[0]\n&quot;;  
     chdir $_[0];  
     opendir(DIR, &quot;.&quot;);  
     my @flist = readdir(DIR);  
     # Delete any existing symbolic links  
     foreach (grep {/^[\da-f]+\.r{0,1}\d+$/} @flist) {  
         if(-l $_) {  
             unlink $_;  
         }  
     }  
     closedir DIR;  
     FILE: foreach $fname (grep {/\.pem$/} @flist) {  
         # Check to see if certificates and/or CRLs present.  
         my ($cert, $crl) = check_file($fname);  
         if(!$cert &amp;amp;&amp;amp; !$crl) {  
             print STDERR &quot;WARNING: $fname does not contain a certificate or CRL: skipping\n&quot;;  
             next;  
         }  
         link_hash_cert($fname) if($cert);  
+        link_hash_cert_compat($fname) if($cert);  
         link_hash_crl($fname) if($crl);  
     }  
 }  
&lt;/code&gt;&lt;/pre&gt;
</content><link rel='replies' type='application/atom+xml' href='http://sophie-notes.blogspot.com/feeds/6114993634591941969/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/8742529245277406456/6114993634591941969' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8742529245277406456/posts/default/6114993634591941969'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8742529245277406456/posts/default/6114993634591941969'/><link rel='alternate' type='text/html' href='http://sophie-notes.blogspot.com/2013/03/ssl-validation-notes-lwpuseragent-58-vs.html' title='SSL Validation Notes: LWP::UserAgent (5.8 vs. 6.0) and Openssl (0.9.8 vs. 1.0.0)'/><author><name>Sophie</name><uri>http://www.blogger.com/profile/08130938292479634191</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgJYm9W1Baj1dfsHW3J9uikPP0eW2P-JuM4uRrJUZI8b-V2_H1C4tGNKzw_wTUUDzxo6VN9r7LKCHoG4ApDuPcc_X2oqejeV_pL9J1K5ezSnXluTppUj60ZBQQBjt73dA/s220/12.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8742529245277406456.post-6003231055941568673</id><published>2013-02-26T00:29:00.006-08:00</published><updated>2013-08-18T00:42:05.634-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Mathimatical modeling"/><category scheme="http://www.blogger.com/atom/ns#" term="Programming"/><title type='text'>Bit Manipulation Tutorial II</title><content type='html'>&lt;i&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;[Ref:&amp;nbsp;http://www.matrix67.com/blog/archives/264]&lt;/span&gt;&lt;/i&gt;&lt;br /&gt;
&lt;i&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;[Further reading:&amp;nbsp;&lt;a href=&quot;http://graphics.stanford.edu/~seander/bithacks.html&quot; target=&quot;_blank&quot;&gt;Bit Twiddling Hacks (Stanford.edu)&lt;/a&gt;;&amp;nbsp;&lt;a href=&quot;http://spellscroll.com/spellscrolls/question/Q9qadszJuac/&quot; target=&quot;_blank&quot;&gt;Bit manipulation questions&lt;/a&gt;]&lt;/span&gt;&lt;/i&gt;&lt;br /&gt;
&lt;div&gt;
&lt;h3&gt;
Advanced Problems 1: Swap high and low digits.&lt;/h3&gt;
&lt;/div&gt;
&lt;h4&gt;
Problem&lt;/h4&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;i&gt;Given a 32-bit integer, swap the high 16 bits and low 16 bits of its binary correspondence.&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;i&gt;For example, given 1314520 = 0b00000000 00010100 00001110 11011000, return 249036820 = 0b00001110 11011000 00000000 00010100.&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;
&lt;h4&gt;
Solution&lt;/h4&gt;
&lt;div&gt;
This one is simple.&lt;/div&gt;
&lt;pre style=&quot;background-color: #f0f0f0; border: 1px dashed rgb(204, 204, 204); font-family: arial; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; width: 646.46875px;&quot;&gt;&lt;code style=&quot;word-wrap: normal;&quot;&gt;int swap(int x) {
    return (x &amp;gt;&amp;gt; 16) | (x &amp;lt;&amp;lt; 16);
}&lt;/code&gt;&lt;/pre&gt;
&lt;div&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;h3&gt;
Advanced Problems 2: Implement Math.abs() with bit operations.&lt;/h3&gt;
&lt;/div&gt;
&lt;h4&gt;
Problem&lt;/h4&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;i&gt;Given an integer, return its absolute value, only using bit operations.&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;
&lt;h4&gt;
Solution&lt;/h4&gt;
&lt;div&gt;
How is an integer represented in memory?&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
An integer uses its most significant bit as the sign. Thus, a positive number has 0 on its most significant bit while negative one has 1 on that bit.&lt;br /&gt;
For the rest bits, negative number is obtained by taking 1&#39;s Complement of positive number and adding 1. E.g. -1 = 0xFFFFFFFF.&lt;br /&gt;
&lt;br /&gt;
So,&amp;nbsp;&lt;code&gt;x &amp;gt;&amp;gt; 31&lt;/code&gt;&amp;nbsp;is the sign, if it is 0, means that x is positive; if it is 1, negative.&lt;br /&gt;
For negative numbers we need to flip all bits and add one; For positive ones, leave it as it is.&lt;br /&gt;
Note that there are two ways to flip all bits,&amp;nbsp;&lt;code&gt;~x&lt;/code&gt;&amp;nbsp;and&amp;nbsp;&lt;code&gt;x^0xFFFFFFFF&lt;/code&gt;.&lt;/div&gt;
&lt;pre style=&quot;background-color: #f0f0f0; border: 1px dashed rgb(204, 204, 204); font-family: arial; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; width: 646.46875px;&quot;&gt;&lt;code style=&quot;word-wrap: normal;&quot;&gt;int abs(int x) {
    if (x &amp;gt;&amp;gt; 31 != 0)  return (~x + 1);
    return x;
}&lt;/code&gt;&lt;/pre&gt;
Algorithm with pure bit manipulations:&lt;br /&gt;
&lt;pre style=&quot;background-color: #f0f0f0; border: 1px dashed rgb(204, 204, 204); font-family: arial; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; width: 646.46875px;&quot;&gt;&lt;code style=&quot;word-wrap: normal;&quot;&gt;int abs(int x) {
    return x ^ (~(x &amp;gt;&amp;gt; 31) + 1) + x &amp;gt;&amp;gt; 31;
}&lt;/code&gt;&lt;/pre&gt;
&lt;div&gt;
&lt;/div&gt;
&lt;h3&gt;
Advanced Problems 3: How many Leading 0&#39;s?&lt;/h3&gt;
&lt;h4&gt;
Problem&lt;/h4&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;i&gt;Given a 32-bit integer, find out how many leading zeros in its binary format.&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;i&gt;For example, given 1314520 = 0b00000000 00010100 00001110 11011000, return 11.&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;
&lt;h4&gt;
Solution&lt;/h4&gt;
We use Binary Search to solve this problem.&lt;br /&gt;
&lt;br /&gt;
The basic idea is&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;Does the high 16 bits are 0&#39;s? If yes, check the low 16 bits; otherwise, check the high 16 bits.&lt;/li&gt;
&lt;li&gt;Does the high 8 bits are 0&#39;s? If yes, check the low 8 bits; otherwise, check the high 8 bits.&lt;/li&gt;
&lt;li&gt;...&lt;/li&gt;
&lt;li&gt;Similarly, continue onto 4 bits, 2 bits and 1 bit that we are interested in.&lt;/li&gt;
&lt;/ul&gt;
The algorithm is as follows.&lt;br /&gt;
&lt;pre style=&quot;background-color: #f0f0f0; border: 1px dashed rgb(204, 204, 204); font-family: arial; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; width: 646.46875px;&quot;&gt;&lt;code style=&quot;word-wrap: normal;&quot;&gt;int leadingZeros(int x) {
    if (x == 0)  return 32;
    int n = 0;
    if (x&amp;gt;&amp;gt;16 == 0) { n += 16; x = x &amp;lt;&amp;lt; 16; };
    if (x&amp;gt;&amp;gt;24 == 0) { n += 8; x = x &amp;lt;&amp;lt; 8; };
    if (x&amp;gt;&amp;gt;28 == 0) { n += 4; x = x &amp;lt;&amp;lt; 4; };
    if (x&amp;gt;&amp;gt;30 == 0) { n += 2; x = x &amp;lt;&amp;lt; 2; };
    if (x&amp;gt;&amp;gt;31 == 0) { n += 1; };
    return n;
}&lt;/code&gt;&lt;/pre&gt;
&lt;div&gt;
&lt;/div&gt;
&lt;h3&gt;
Advanced Problems 4: How many 1&#39;s?&lt;/h3&gt;
&lt;h4&gt;
Problem&lt;/h4&gt;
&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;i&gt;Given a 32-bit integer, find the number of 1&#39;s in its binary format.&lt;/i&gt;&lt;/span&gt;&lt;br /&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;i&gt;Take an 8-bit integer as an example. Given 0b10110111, return 0b110 = 6.&lt;/i&gt;&lt;/span&gt;&lt;br /&gt;
&lt;h4&gt;
Solution&lt;/h4&gt;
We solve the problem with Divide-and-Conquer algorithms.&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;Count the number of 1&#39;s in every 2 adjacent digits;&lt;/li&gt;
&lt;/ul&gt;
&lt;blockquote class=&quot;tr_bq&quot;&gt;
&lt;code&gt;x &amp;amp; 0x55555555 + (x &amp;gt;&amp;gt; 1) &amp;amp; 0x55555555&lt;/code&gt;&lt;/blockquote&gt;
&lt;blockquote class=&quot;tr_bq&quot;&gt;
Note that &lt;code&gt;0x5 = &amp;nbsp;0b0101&lt;/code&gt;.&lt;br /&gt;
&lt;code&gt;(x &amp;amp; 0x55555555)&lt;/code&gt; is to get the numbers on 0, 2, 4, ..., 30 even bits. Then shift x to its right and get the number on odd bits.&lt;br /&gt;
Adding them together gives us the number of 1&#39;s in adjacent two digits.&lt;br /&gt;
&lt;code&gt;10 11 01 11 -&amp;gt; &amp;nbsp;01 10 01 10&lt;/code&gt;&lt;/blockquote&gt;
&lt;ul&gt;
&lt;li&gt;Count the number of 1&#39;s in every 4 adjacent digits;&lt;/li&gt;
&lt;/ul&gt;
&lt;blockquote class=&quot;tr_bq&quot;&gt;
&lt;code&gt;x &amp;amp; 0x33333333 + (x &amp;gt;&amp;gt; 2) &amp;amp; 0x33333333&lt;/code&gt;&lt;/blockquote&gt;
&lt;blockquote class=&quot;tr_bq&quot;&gt;
&lt;code&gt;0x3 = 0b0011&lt;/code&gt;.&lt;br /&gt;
This time we select the two digits sums from the previous result and add them up.&lt;br /&gt;
&lt;code&gt;01 10 01 10 &amp;nbsp;-&amp;gt; &amp;nbsp;00 11 00 11&lt;/code&gt;&lt;/blockquote&gt;
&lt;ul&gt;
&lt;li&gt;Count the number of 1&#39;s in every 8 adjacent digits;&lt;/li&gt;
&lt;/ul&gt;
&lt;div&gt;
&lt;blockquote class=&quot;tr_bq&quot;&gt;
&lt;code&gt;x &amp;amp; 0x0F0F0F0F + (x &amp;gt;&amp;gt; 4) &amp;amp; 0x0F0F0F0F&lt;/code&gt;&lt;/blockquote&gt;
&lt;blockquote class=&quot;tr_bq&quot;&gt;
&lt;code&gt;0x0F = 0b00001111&lt;/code&gt;.&lt;br /&gt;
This time we select the four digits sums from the previous result and add them up.&lt;br /&gt;
&lt;code&gt;00 11 00 11 &amp;nbsp;-&amp;gt; &amp;nbsp;00 00 01 10&lt;/code&gt;&lt;/blockquote&gt;
&lt;ul&gt;
&lt;li&gt;Count the number of 1&#39;s in every 16 adjacent digits;&lt;/li&gt;
&lt;/ul&gt;
&lt;div&gt;
&lt;blockquote class=&quot;tr_bq&quot;&gt;
&lt;code&gt;x &amp;amp; 0x00FF00FF + (x &amp;gt;&amp;gt; 8) &amp;amp; 0x00FF00FF&lt;/code&gt;&lt;/blockquote&gt;
&lt;/div&gt;
&lt;ul&gt;
&lt;li&gt;Count the number of 1&#39;s in 32 digits;&lt;/li&gt;
&lt;/ul&gt;
&lt;div&gt;
&lt;blockquote class=&quot;tr_bq&quot;&gt;
&lt;code&gt;x &amp;amp; 0x0000FFFF + (x &amp;gt;&amp;gt; 16) &amp;amp; 0x0000FFFF&lt;/code&gt;&lt;/blockquote&gt;
The final algorithm looks like this:&lt;br /&gt;
&lt;pre style=&quot;background-image: URL(https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiqsl8vm2ODGqxjnZwd7bqtBugZa1EOe4r3nFGP9ErfkJRPdYStTvkuoNm_8pXw-W_126q5s1dWXJ93It7AszOaCgpKagDLlNQC__kF7lWOw6TV-wMnL0S0DLhwKUhkYPqDKAJc4DTKu29C/s320/codebg.gif); background: #f0f0f0; border: 1px dashed #CCCCCC; color: black; font-family: arial; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;&quot;&gt;&lt;code style=&quot;color: black; word-wrap: normal;&quot;&gt;int findOnes(int x) {
    x = (x &amp;amp;amp; 0x55555555) + ((x &amp;amp;gt;&amp;amp;gt; 1) &amp;amp;amp; 0x55555555);  
    x = (x &amp;amp;amp; 0x33333333) + ((x &amp;amp;gt;&amp;amp;gt; 2) &amp;amp;amp; 0x33333333);  
    x = (x &amp;amp;amp; 0x0F0F0F0F) + ((x &amp;amp;gt;&amp;amp;gt; 4) &amp;amp;amp; 0x0F0F0F0F);  
    x = (x &amp;amp;amp; 0x00FF00FF) + ((x &amp;amp;gt;&amp;amp;gt; 8) &amp;amp;amp; 0x00FF00FF);  
    x = (x &amp;amp;amp; 0x0000FFFF) + ((x &amp;amp;gt;&amp;amp;gt; 16) &amp;amp;amp; 0x0000FFFF);  
    return x;
}&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;h3&gt;
Advanced Problems 5: Odd 1&#39;s or Even 1&#39;s?&lt;/h3&gt;
&lt;h4&gt;
Problem&lt;/h4&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;i&gt;Given a 32-bit integer, if there are even number of 1&#39;s in its binary format, return 0; otherwise, return 1.&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;i&gt;For example, given 1314520 = 0b10100 00001110 11011000, return 1 (it has nine 1&#39;s).&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;
&lt;h4&gt;
Solution&lt;/h4&gt;
Again, we use Divide-and-Conquer.&lt;br /&gt;
&lt;br /&gt;
Given two bits, if xor returns 1, means that there are one 1; otherwise, there are even number of 1&#39;s (either 0 or 2).&lt;br /&gt;
The key points are:&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;In the result of &lt;code&gt;x ^ (x &amp;gt;&amp;gt; 1)&lt;/code&gt;, the &lt;i&gt;i&lt;/i&gt;-th bit shows whether there are odd number of 1&#39;s in &lt;i&gt;i&lt;/i&gt; and &lt;i&gt;i+1&lt;/i&gt; bits of original &lt;i&gt;x&lt;/i&gt;.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;x ^ (x &amp;gt;&amp;gt; k)&lt;/code&gt;, the &lt;i&gt;i&lt;/i&gt;-th bit shows whether there are odd number of 1&#39;s in &lt;i&gt;i&lt;/i&gt; and &lt;i&gt;i+k&lt;/i&gt; bits of original &lt;i&gt;x&lt;/i&gt;.&lt;/li&gt;
&lt;li&gt;odd + odd = even; &amp;nbsp;even + even = even; &amp;nbsp;odd + even = odd.&lt;/li&gt;
&lt;/ul&gt;
The final algorithm looks like this.&lt;br /&gt;
&lt;pre style=&quot;background-image: URL(https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiqsl8vm2ODGqxjnZwd7bqtBugZa1EOe4r3nFGP9ErfkJRPdYStTvkuoNm_8pXw-W_126q5s1dWXJ93It7AszOaCgpKagDLlNQC__kF7lWOw6TV-wMnL0S0DLhwKUhkYPqDKAJc4DTKu29C/s320/codebg.gif); background: #f0f0f0; border: 1px dashed #CCCCCC; color: black; font-family: arial; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;&quot;&gt;&lt;code&gt;&lt;code&gt;&lt;code&gt;&lt;code style=&quot;color: black; word-wrap: normal;&quot;&gt;boolean isOddOnes(int x) {
    x = x ^ (x &amp;gt;&amp;gt; 1);  
    x = x ^ (x &amp;gt;&amp;gt; 2);  
    x = x ^ (x &amp;gt;&amp;gt; 4);  
    x = x ^ (x &amp;gt;&amp;gt; 8);  
    x = x ^ (x &amp;gt;&amp;gt; 16);  
    return (x &amp;amp; 1);
}&lt;/code&gt;&lt;/code&gt;&lt;/code&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3&gt;
Advanced Problems 6: Bit Reverse.&lt;/h3&gt;
&lt;h4&gt;
Problem&lt;/h4&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;i&gt;Given a 32-bit integer, reverse all bits and return the corresponding integer.&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;i&gt;For example, given 1314520 = 0b00000000 00010100 00001110 11011000, return 460335104 = 0b 00011011 01110000 00101000 00000000 &lt;/i&gt;&lt;/span&gt;&lt;/div&gt;
&lt;h4&gt;
Solution&lt;/h4&gt;
&lt;div&gt;
We use a similar idea of counting number of 1&#39;s (see Advanced Problem 4).&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
We first swap the adjacent bits, and then swap 2 bits, 4 bits, 8 bits, and 16 bits.&lt;/div&gt;
&lt;div&gt;
For instance, if we have ABCDEFGH. It will be BADCFEHG -&amp;gt; DCBAHGFE -&amp;gt; HGFEDCBA. Tada!&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
Recall that we can use and to pick up the bits we want and then shift them to the positions we want.&lt;/div&gt;
The final algorithm looks like this.&lt;br /&gt;
&lt;pre style=&quot;background-image: URL(https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiqsl8vm2ODGqxjnZwd7bqtBugZa1EOe4r3nFGP9ErfkJRPdYStTvkuoNm_8pXw-W_126q5s1dWXJ93It7AszOaCgpKagDLlNQC__kF7lWOw6TV-wMnL0S0DLhwKUhkYPqDKAJc4DTKu29C/s320/codebg.gif); background: #f0f0f0; border: 1px dashed #CCCCCC; color: black; font-family: arial; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;&quot;&gt;&lt;code&gt;&lt;code&gt;&lt;code&gt;&lt;code style=&quot;color: black; word-wrap: normal;&quot;&gt;boolean reverseBits(int x) {
    x = ((x &amp;amp; 0x55555555) &amp;lt;&amp;lt; 1) | ((x &amp;amp; 0xAAAAAAAA) &amp;gt;&amp;gt; 1);  
    x = ((x &amp;amp; 0x33333333) &amp;lt;&amp;lt; 2) | ((x &amp;amp; 0xCCCCCCCC) &amp;gt;&amp;gt; 2);  
    x = ((x &amp;amp; 0x0F0F0F0F) &amp;lt;&amp;lt; 4) | ((x &amp;amp; 0xF0F0F0F0) &amp;gt;&amp;gt; 4);  
    x = ((x &amp;amp; 0x00FF00FF) &amp;lt;&amp;lt; 8) | ((x &amp;amp; 0xFF00FF00) &amp;gt;&amp;gt; 8);  
    x = ((x &amp;amp; 0x0000FFFF) &amp;lt;&amp;lt; 16) | ((x &amp;amp; 0xFFFF0000) &amp;gt;&amp;gt; 16);  
    return x;
}&lt;/code&gt;&lt;/code&gt;&lt;/code&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;h3&gt;
Advanced Problem 7: Find Log Base 2 of an Integer.&lt;/h3&gt;
&lt;h4&gt;
Problem&lt;/h4&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;i&gt;Given a 32-bit integer n, return log_2^n.&lt;/i&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;i&gt;For example, given n = 18, return 4.&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;h4&gt;
Solution&lt;/h4&gt;
&lt;/div&gt;
&lt;div&gt;
The log base 2 of an integer is the same as the position of the most significant bit of the integer.&lt;br /&gt;
&lt;br /&gt;
An straightforward way is to shift the number bit by bit and perform the calculation.&lt;/div&gt;
&lt;pre style=&quot;background: #f0f0f0; border: 1px dashed #CCCCCC; color: black; font-family: arial; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;&quot;&gt;&lt;code style=&quot;color: black; word-wrap: normal;&quot;&gt; /* floor(log(n) */  
 public int log(int n) {  
   int d = 0;  
   while (n &amp;gt; 1) {  
     n &amp;gt;&amp;gt;= 1;  
     ++d;  
   }  
   return d;  
 }  
&lt;/code&gt;&lt;/pre&gt;
This algorithm runs in time O(&lt;i&gt;n&lt;/i&gt;) time.&lt;br /&gt;
&lt;br /&gt;
Since we already know that the given number is a 32-bit integer, we can use Divide-and-Conquer too.&lt;br /&gt;
&lt;pre style=&quot;background: #f0f0f0; border: 1px dashed #CCCCCC; color: black; font-family: arial; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;&quot;&gt;&lt;code style=&quot;color: black; word-wrap: normal;&quot;&gt; /* floor(log(n) */  
 public static int log(int n) {  
   final int[] masks = {0x2, 0xC, 0xF0, 0xFF00, 0xFFFF0000};  
   final int[] lens = {1, 2, 4, 8, 16};  
   
   int d = 0;  
   for (int i=masks.length-1; i&amp;gt;=0; --i) {  
     if ((n &amp;amp; masks[i]) &amp;gt; 0) {  
       d += lens[i];  
       n &amp;gt;&amp;gt;= lens[i];  
     }  
   }  
   return d;  
 }  
&lt;/code&gt;&lt;/pre&gt;
</content><link rel='replies' type='application/atom+xml' href='http://sophie-notes.blogspot.com/feeds/6003231055941568673/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/8742529245277406456/6003231055941568673' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8742529245277406456/posts/default/6003231055941568673'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8742529245277406456/posts/default/6003231055941568673'/><link rel='alternate' type='text/html' href='http://sophie-notes.blogspot.com/2013/02/bit-manipulation-tutorial-ii.html' title='Bit Manipulation Tutorial II'/><author><name>Sophie</name><uri>http://www.blogger.com/profile/08130938292479634191</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgJYm9W1Baj1dfsHW3J9uikPP0eW2P-JuM4uRrJUZI8b-V2_H1C4tGNKzw_wTUUDzxo6VN9r7LKCHoG4ApDuPcc_X2oqejeV_pL9J1K5ezSnXluTppUj60ZBQQBjt73dA/s220/12.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8742529245277406456.post-4014878129887503167</id><published>2013-02-25T00:39:00.000-08:00</published><updated>2013-08-11T16:20:13.222-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Mathimatical modeling"/><category scheme="http://www.blogger.com/atom/ns#" term="Programming"/><title type='text'>Bit Manipulation Tutorial I</title><content type='html'>&lt;a href=&quot;http://en.wikipedia.org/wiki/Bit_manipulation&quot; target=&quot;_blank&quot;&gt;Bit&amp;nbsp;Manipulation&lt;/a&gt;&amp;nbsp;is important in algorithms, especially in error detection and correction algorithms, data compression algorithms, encryption algorithms, and optimization.&lt;br /&gt;
&lt;br /&gt;
It can make programs simple and elegant, and needless to say, it is fast since that how computer works. E.g. a &amp;lt;&amp;lt; 1 will be slightly faster than a*2 (most of modern compilers may change the latter to the former now). Well, sometimes it may lose some readability by using bit operations in&amp;nbsp;programs.&lt;br /&gt;
&lt;h3&gt;
What is Bit Manipulation?&lt;/h3&gt;
As is well known, numbers are stored and operated as binaries inside a computer. &amp;nbsp;So, bit manipulation, as named, is to manipulate binary bits of a number directly.&lt;br /&gt;
E.g. 6 &amp;amp; 11 = 0x110 &amp;amp; 0x1011 = 0x0001 = 2&lt;br /&gt;
&lt;h3&gt;
Bit Operations&lt;/h3&gt;
&lt;h4&gt;
and (&amp;amp;) operator - &quot;are they all 1?&quot;&lt;/h4&gt;
&lt;blockquote class=&quot;tr_bq&quot;&gt;
0 &amp;amp; 0 = 0, &amp;nbsp;1 &amp;amp; 1 = 1, 1 &amp;amp; 0 = 0 &amp;amp; 1 = 0&lt;/blockquote&gt;
Examples&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;What&#39;s the k-th digit of x?&lt;br /&gt;
&lt;code&gt;(x &amp;gt;&amp;gt; (k-1)) &amp;amp; 1&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Is a number odd? (I.e., Is the last digit 1?)&lt;br /&gt;
&lt;code&gt;(x &amp;amp; 1) == 1&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;What&#39;s the last k digits of x?&lt;br /&gt;
&lt;code&gt;x &amp;amp; (1 &amp;lt;&amp;lt; k - 1)&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;What&#39;s the last 3 digits of x?&lt;br /&gt;
&lt;code&gt;x &amp;amp; 0x111&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Set the k-th digit to 0, e.g. 0x101101 -&amp;gt; 0x101001, k=3&lt;br /&gt;
&lt;code&gt;x &amp;amp; ~(1 &amp;lt;&amp;lt; (k-1))&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Is a number pow of 2?&lt;br /&gt;
&lt;code&gt;(x != 0) &amp;amp;&amp;amp; (x &amp;amp; (x-1) ==0),&amp;nbsp;i.e. x &amp;amp;&amp;amp; !(x &amp;amp; (x - 1))&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Flip the last&amp;nbsp;continuous&amp;nbsp;1&#39;s to 0&#39;s, e.g. 0b10101111 -&amp;gt; 0b10100000&lt;br /&gt;
&lt;code&gt;x &amp;amp; (x+1)&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h4&gt;
or (|) operator - &quot;is any of them 1?&quot;&lt;/h4&gt;
&lt;blockquote class=&quot;tr_bq&quot;&gt;
0 &amp;amp; 0 = 0, &amp;nbsp;1 &amp;amp; 1 = 1, 1 &amp;amp; 0 = 0 &amp;amp; 1 = 1&lt;/blockquote&gt;
&lt;div&gt;
Examples&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;Set the last digit to 1&lt;br /&gt;
&lt;code&gt;x | 1&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Set the number to its closest even number (i.e. set the last digit to 0)&lt;br /&gt;
&lt;code&gt;(x | 1) - 1&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Set the k-th digit to 1, e.g. 0b101001 -&amp;gt; 0b101101, k=3&lt;br /&gt;
&lt;code&gt;x | (1 &amp;lt;&amp;lt; (k-1))&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Set the last k digits to 1, e.g. 0b101001 -&amp;gt; 0b101111, k=3&lt;br /&gt;
&lt;code&gt;x | (1 &amp;lt;&amp;lt; k - 1)&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Flip&amp;nbsp;the last&amp;nbsp;continuous&amp;nbsp;0&#39;s to 1&#39;s, e.g. 0b10110000 -&amp;gt; 0b10111111&lt;br /&gt;
&lt;code&gt;x | (x-1)&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Flip&amp;nbsp;the first 0 to 1 (starting from right), e.g. 0b10101011 -&amp;gt; 0b10101111&lt;br /&gt;
&lt;code&gt;x | (x+1)&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;h4&gt;
xor (^) operator - &quot;are they different?&quot;&lt;/h4&gt;
&lt;blockquote class=&quot;tr_bq&quot;&gt;
0 &amp;amp; 0 = 0, &amp;nbsp;1 &amp;amp; 1 = 0, 1 &amp;amp; 0 = 0 &amp;amp; 1 = 1&lt;/blockquote&gt;
Also, notice that xor&#39;s inverse operation is itself! That is, (x ^ y) ^ x = y&lt;br /&gt;
Examples&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;Flip k-th digit&lt;br /&gt;
&lt;code&gt;x ^ (1 &amp;lt;&amp;lt; (k-1))&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Flip last k digits&lt;br /&gt;
&lt;code&gt;x ^ (1 &amp;lt;&amp;lt; k - 1)&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Get the last continuous 1&#39;s, e.g. 0b10101111 -&amp;gt; 0b1111&lt;br /&gt;
&lt;code&gt;(x ^ (x+1)) &amp;gt;&amp;gt; 1&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Remove the left of the first 1 from the right, e.g. 0b10101000 -&amp;gt; 0b1000&lt;br /&gt;
&lt;code&gt;(x ^ (x-1)) &amp;gt;&amp;gt; 1 + 1&lt;/code&gt;&amp;nbsp; &amp;nbsp;or &amp;nbsp;&amp;nbsp;&lt;code&gt;x ^ (x-1) &amp;amp; x&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Swap two numbers&lt;br /&gt;
One way to swap two numbers is to use + and - (two inverse operations):&lt;br /&gt;
&lt;code&gt;a = a + b; &amp;nbsp;b = a - b; &amp;nbsp;a = a - b;&lt;/code&gt;&lt;br /&gt;
And we can use xor and since its inverse operation is itself:&lt;br /&gt;
&lt;code&gt;a = a ^ b; &amp;nbsp;b = a ^ b; &amp;nbsp;a = a ^ b;&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h4&gt;
not (~) operator&lt;/h4&gt;
&lt;blockquote class=&quot;tr_bq&quot;&gt;
~0 = 1, &amp;nbsp;~1 = 0&lt;/blockquote&gt;
&lt;div&gt;
Examples&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;Flip all digits of an unsigned number&lt;br /&gt;
&lt;code&gt;~x&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;h4&gt;
shifting (&amp;gt;&amp;gt;, &amp;lt;&amp;lt;) operators&lt;/h4&gt;
&lt;blockquote class=&quot;tr_bq&quot;&gt;
x &amp;gt;&amp;gt; k, shift right by k digits, i.e. divide by 2^k (note: ^ is not xor here... :)&lt;/blockquote&gt;
&lt;blockquote class=&quot;tr_bq&quot;&gt;
x &amp;lt;&amp;lt; k, shift left by k digits, i.e. multiple by 2^k&lt;/blockquote&gt;
Examples&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;Remove the last digit&lt;br /&gt;
&lt;code&gt;x &amp;gt;&amp;gt; 1&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Add a 0 to the end of a number&lt;br /&gt;
&lt;code&gt;x &amp;lt;&amp;lt; 1&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Add an 1 to the end of a number&lt;br /&gt;
&lt;code&gt;x &amp;lt;&amp;lt; 1 + 1&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
</content><link rel='replies' type='application/atom+xml' href='http://sophie-notes.blogspot.com/feeds/4014878129887503167/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/8742529245277406456/4014878129887503167' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8742529245277406456/posts/default/4014878129887503167'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8742529245277406456/posts/default/4014878129887503167'/><link rel='alternate' type='text/html' href='http://sophie-notes.blogspot.com/2013/02/bit-manipulation-tutorial-i.html' title='Bit Manipulation Tutorial I'/><author><name>Sophie</name><uri>http://www.blogger.com/profile/08130938292479634191</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgJYm9W1Baj1dfsHW3J9uikPP0eW2P-JuM4uRrJUZI8b-V2_H1C4tGNKzw_wTUUDzxo6VN9r7LKCHoG4ApDuPcc_X2oqejeV_pL9J1K5ezSnXluTppUj60ZBQQBjt73dA/s220/12.jpg'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8742529245277406456.post-9048786737129121595</id><published>2013-02-04T00:27:00.001-08:00</published><updated>2013-08-11T00:11:04.196-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Java"/><category scheme="http://www.blogger.com/atom/ns#" term="Programming"/><title type='text'>Java StringBuilder vs. String</title><content type='html'>In Java, we should always use &lt;code&gt;&lt;a href=&quot;http://docs.oracle.com/javase/6/docs/api/java/lang/StringBuilder.html&quot; target=&quot;_blank&quot;&gt;StringBuilder&lt;/a&gt; append()&lt;/code&gt;&amp;nbsp;method rather than&amp;nbsp;&lt;code&gt;String &quot;+&quot; operator&lt;/code&gt;&amp;nbsp;to avoid cost. Note that what &lt;code&gt;&quot;+&quot; operator&lt;/code&gt; does is:&lt;br /&gt;
&lt;ol&gt;
&lt;li&gt;create a new string;&lt;/li&gt;
&lt;li&gt;make a copy of the left string;&lt;/li&gt;
&lt;li&gt;append the right char/string to the end.&lt;/li&gt;
&lt;/ol&gt;
So, a series of &quot;+&quot; operation is O(&lt;i&gt;n^2&lt;/i&gt;)-time, not O(&lt;i&gt;n&lt;/i&gt;).&lt;br /&gt;
&lt;br /&gt;
&lt;code&gt;StringBuilder&lt;/code&gt; is smarter and you can think it as a dynamically resizing array of char. No copies for &lt;code&gt;append()&lt;/code&gt; and it can output a&amp;nbsp;&lt;code&gt;String&lt;/code&gt;&amp;nbsp;by calling &lt;code&gt;toString()&lt;/code&gt; method.&lt;br /&gt;
&lt;br /&gt;
&lt;i&gt;Note: StringBuilder has no synchronization, i.e. it is not thread-safe. If synchronization is required, it is recommend to use &lt;code&gt;&lt;a href=&quot;http://docs.oracle.com/javase/6/docs/api/java/lang/StringBuffer.html&quot;&gt;StringBuffer&lt;/a&gt;&lt;/code&gt;. Both of them support the same set of operations. But, for use cases in single thread, StringBuilder is preferred since it will faster since it performs no synchronization.&lt;/i&gt;</content><link rel='replies' type='application/atom+xml' href='http://sophie-notes.blogspot.com/feeds/9048786737129121595/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/8742529245277406456/9048786737129121595' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8742529245277406456/posts/default/9048786737129121595'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8742529245277406456/posts/default/9048786737129121595'/><link rel='alternate' type='text/html' href='http://sophie-notes.blogspot.com/2013/02/java-stringbuilder-vs-string.html' title='Java StringBuilder vs. String'/><author><name>Sophie</name><uri>http://www.blogger.com/profile/08130938292479634191</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgJYm9W1Baj1dfsHW3J9uikPP0eW2P-JuM4uRrJUZI8b-V2_H1C4tGNKzw_wTUUDzxo6VN9r7LKCHoG4ApDuPcc_X2oqejeV_pL9J1K5ezSnXluTppUj60ZBQQBjt73dA/s220/12.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8742529245277406456.post-8157536677643390991</id><published>2012-12-30T17:30:00.000-08:00</published><updated>2013-03-06T21:37:23.411-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Java"/><category scheme="http://www.blogger.com/atom/ns#" term="Programming"/><category scheme="http://www.blogger.com/atom/ns#" term="STL study"/><title type='text'>Java ArrayList Capacity Increment Policy</title><content type='html'>It is well known that Java ArrayList is a dynamically resizable array. From ArrayList&#39;s&amp;nbsp;&lt;a href=&quot;http://docs.oracle.com/javase/6/docs/api/java/util/ArrayList.html&quot; target=&quot;_blank&quot;&gt;java doc&lt;/a&gt;:&lt;br /&gt;
&lt;blockquote class=&quot;tr_bq&quot;&gt;
&lt;span style=&quot;background-color: white; font-family: Courier New, Courier, monospace;&quot;&gt;The details of the growth policy are not specified beyond the fact that adding an element has constant amortized time cost.&lt;/span&gt;&lt;/blockquote&gt;
But it may be not-so-well-known that it is using a different algorithm for capacity increment from the one in the algorithm textbook CLR.&lt;br /&gt;
&lt;h4&gt;
The Capacity Increment Policy in CLR (and STL vector)&lt;/h4&gt;
Recall that, in CLR, it uses dynamic table as an example for amortize analysis, where the table doubles when it&#39;s full and reduce to half when getting 1/4 empty. By doing this, the add/insert operation runs in amortize constant time and reallocations happen at log_2^n intervals where n is the size of current elements in the table.&lt;br /&gt;
&lt;br /&gt;
STL &lt;a href=&quot;http://www.cplusplus.com/reference/vector/vector/&quot; target=&quot;_blank&quot;&gt;vector&lt;/a&gt; uses this implementation for growing policy.&lt;br /&gt;
&lt;h4&gt;
The Capacity Increment Policy of Java ArrayList&lt;/h4&gt;
ArrayList uses a same algorithm but different growing ratio: increased by ~50% of old capacity.&lt;br /&gt;
&lt;blockquote class=&quot;tr_bq&quot;&gt;
Java 1.7 &amp;nbsp;&lt;code&gt;int newCapacity = oldCapacity + (oldCapacity &amp;gt;&amp;gt; 1)&lt;/code&gt;&lt;br /&gt;
Java 6 &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;code&gt;int newCapacity = (oldCapacity * 3)/2 + 1;&lt;/code&gt;&lt;/blockquote&gt;
One (possible) reason for Java to use such a policy is to&amp;nbsp;sacrifice&amp;nbsp;reallocations frequencies for better memory usage. See &lt;a href=&quot;http://stackoverflow.com/a/3336510/1131594&quot; target=&quot;_blank&quot;&gt;discussions&lt;/a&gt; in stackoverflow.&lt;br /&gt;
Now the growing interval goes up to ~log_{3/2}^n ~= 4.45*log_2^n. Still, it provides amortize constant time for insertions but the constant is&amp;nbsp;higher, especially for small n&#39;s. To&amp;nbsp;compromise&amp;nbsp;this, ArrayList starts with a default initial capacity of 10.&lt;br /&gt;
&lt;br /&gt;
Attached below is a table of comparisons between the two increment policies. As shown in the table,&lt;br /&gt;
&lt;ol&gt;
&lt;li&gt;ArrayList requires less extra space than vector.&lt;/li&gt;
&lt;li&gt;ArrayList requires more reallocations than vector.&lt;/li&gt;
&lt;li&gt;By starting with a capacity of 10, ArrayList reduces several reallocations at the beginning.&lt;/li&gt;
&lt;/ol&gt;
&lt;div&gt;
Remember that each relocation means a copy of&amp;nbsp;&lt;b&gt;all&lt;/b&gt;&amp;nbsp;existing contents in the array.&amp;nbsp;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;i&gt;Note that vector provides a method to get the current capacity but ArrayList doesn&#39;t. So the results below for ArrayList are calculated based on the source code of Java 6.&lt;/i&gt;&lt;br /&gt;
&lt;style type=&quot;text/css&quot;&gt;
table.tableizer-table {
 border: 2px solid #CCC; font-family: Arial, Helvetica, sans-serif;
 font-size: 12px;
} 
.tableizer-table td {
 padding: 4px;
 margin: 3px;
 border: 1px solid #ccc;
}
.tableizer-table th {
 background-color: #104E8B; 
 color: #FFF;
 font-weight: bold;
}
&lt;/style&gt;&lt;br /&gt;
&lt;table class=&quot;tableizer-table&quot;&gt;&lt;tbody&gt;
&lt;tr class=&quot;tableizer-firstrow&quot;&gt;&lt;th&gt;&lt;/th&gt;&lt;th&gt;ArrayList&lt;/th&gt;&lt;th&gt;&lt;/th&gt;&lt;th&gt;&lt;/th&gt;&lt;th&gt;vector&lt;/th&gt;&lt;th&gt;&lt;/th&gt;&lt;th&gt;&lt;/th&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;n&lt;/td&gt;&lt;td&gt;capacity&lt;/td&gt;&lt;td&gt;reallocation&lt;/td&gt;&lt;td&gt;extra space&lt;/td&gt;&lt;td&gt;capacity&lt;/td&gt;&lt;td&gt;reallocation&lt;/td&gt;&lt;td&gt;extra space&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;0&lt;/td&gt;&lt;td&gt;10&lt;/td&gt;&lt;td&gt;0&lt;/td&gt;&lt;td&gt;10&lt;/td&gt;&lt;td&gt;0&lt;/td&gt;&lt;td&gt;0&lt;/td&gt;&lt;td&gt;0&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;1&lt;/td&gt;&lt;td&gt;10&lt;/td&gt;&lt;td&gt;0&lt;/td&gt;&lt;td&gt;9&lt;/td&gt;&lt;td&gt;1&lt;/td&gt;&lt;td&gt;1&lt;/td&gt;&lt;td&gt;0&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;2&lt;/td&gt;&lt;td&gt;10&lt;/td&gt;&lt;td&gt;0&lt;/td&gt;&lt;td&gt;8&lt;/td&gt;&lt;td&gt;2&lt;/td&gt;&lt;td&gt;2&lt;/td&gt;&lt;td&gt;0&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;3&lt;/td&gt;&lt;td&gt;10&lt;/td&gt;&lt;td&gt;0&lt;/td&gt;&lt;td&gt;7&lt;/td&gt;&lt;td&gt;4&lt;/td&gt;&lt;td&gt;3&lt;/td&gt;&lt;td&gt;1&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;5&lt;/td&gt;&lt;td&gt;10&lt;/td&gt;&lt;td&gt;0&lt;/td&gt;&lt;td&gt;5&lt;/td&gt;&lt;td&gt;8&lt;/td&gt;&lt;td&gt;4&lt;/td&gt;&lt;td&gt;3&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;9&lt;/td&gt;&lt;td&gt;10&lt;/td&gt;&lt;td&gt;0&lt;/td&gt;&lt;td&gt;1&lt;/td&gt;&lt;td&gt;16&lt;/td&gt;&lt;td&gt;5&lt;/td&gt;&lt;td&gt;7&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;11&lt;/td&gt;&lt;td&gt;16&lt;/td&gt;&lt;td&gt;1&lt;/td&gt;&lt;td&gt;5&lt;/td&gt;&lt;td&gt;16&lt;/td&gt;&lt;td&gt;5&lt;/td&gt;&lt;td&gt;5&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;17&lt;/td&gt;&lt;td&gt;25&lt;/td&gt;&lt;td&gt;2&lt;/td&gt;&lt;td&gt;8&lt;/td&gt;&lt;td&gt;32&lt;/td&gt;&lt;td&gt;6&lt;/td&gt;&lt;td&gt;15&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;26&lt;/td&gt;&lt;td&gt;38&lt;/td&gt;&lt;td&gt;3&lt;/td&gt;&lt;td&gt;12&lt;/td&gt;&lt;td&gt;32&lt;/td&gt;&lt;td&gt;6&lt;/td&gt;&lt;td&gt;6&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;33&lt;/td&gt;&lt;td&gt;38&lt;/td&gt;&lt;td&gt;3&lt;/td&gt;&lt;td&gt;5&lt;/td&gt;&lt;td&gt;64&lt;/td&gt;&lt;td&gt;7&lt;/td&gt;&lt;td&gt;31&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;39&lt;/td&gt;&lt;td&gt;58&lt;/td&gt;&lt;td&gt;4&lt;/td&gt;&lt;td&gt;19&lt;/td&gt;&lt;td&gt;64&lt;/td&gt;&lt;td&gt;7&lt;/td&gt;&lt;td&gt;25&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;59&lt;/td&gt;&lt;td&gt;88&lt;/td&gt;&lt;td&gt;5&lt;/td&gt;&lt;td&gt;29&lt;/td&gt;&lt;td&gt;64&lt;/td&gt;&lt;td&gt;7&lt;/td&gt;&lt;td&gt;5&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;65&lt;/td&gt;&lt;td&gt;88&lt;/td&gt;&lt;td&gt;5&lt;/td&gt;&lt;td&gt;23&lt;/td&gt;&lt;td&gt;128&lt;/td&gt;&lt;td&gt;8&lt;/td&gt;&lt;td&gt;63&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;89&lt;/td&gt;&lt;td&gt;133&lt;/td&gt;&lt;td&gt;6&lt;/td&gt;&lt;td&gt;44&lt;/td&gt;&lt;td&gt;128&lt;/td&gt;&lt;td&gt;8&lt;/td&gt;&lt;td&gt;39&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;129&lt;/td&gt;&lt;td&gt;133&lt;/td&gt;&lt;td&gt;6&lt;/td&gt;&lt;td&gt;4&lt;/td&gt;&lt;td&gt;256&lt;/td&gt;&lt;td&gt;9&lt;/td&gt;&lt;td&gt;127&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;134&lt;/td&gt;&lt;td&gt;200&lt;/td&gt;&lt;td&gt;7&lt;/td&gt;&lt;td&gt;66&lt;/td&gt;&lt;td&gt;256&lt;/td&gt;&lt;td&gt;9&lt;/td&gt;&lt;td&gt;122&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;201&lt;/td&gt;&lt;td&gt;301&lt;/td&gt;&lt;td&gt;8&lt;/td&gt;&lt;td&gt;100&lt;/td&gt;&lt;td&gt;256&lt;/td&gt;&lt;td&gt;9&lt;/td&gt;&lt;td&gt;55&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;256&lt;/td&gt;&lt;td&gt;301&lt;/td&gt;&lt;td&gt;8&lt;/td&gt;&lt;td&gt;45&lt;/td&gt;&lt;td&gt;512&lt;/td&gt;&lt;td&gt;10&lt;/td&gt;&lt;td&gt;256&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;302&lt;/td&gt;&lt;td&gt;452&lt;/td&gt;&lt;td&gt;9&lt;/td&gt;&lt;td&gt;150&lt;/td&gt;&lt;td&gt;512&lt;/td&gt;&lt;td&gt;10&lt;/td&gt;&lt;td&gt;210&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;453&lt;/td&gt;&lt;td&gt;679&lt;/td&gt;&lt;td&gt;10&lt;/td&gt;&lt;td&gt;226&lt;/td&gt;&lt;td&gt;512&lt;/td&gt;&lt;td&gt;10&lt;/td&gt;&lt;td&gt;59&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;513&lt;/td&gt;&lt;td&gt;679&lt;/td&gt;&lt;td&gt;10&lt;/td&gt;&lt;td&gt;166&lt;/td&gt;&lt;td&gt;1024&lt;/td&gt;&lt;td&gt;11&lt;/td&gt;&lt;td&gt;511&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;680&lt;/td&gt;&lt;td&gt;1019&lt;/td&gt;&lt;td&gt;11&lt;/td&gt;&lt;td&gt;339&lt;/td&gt;&lt;td&gt;1024&lt;/td&gt;&lt;td&gt;11&lt;/td&gt;&lt;td&gt;344&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;1020&lt;/td&gt;&lt;td&gt;1529&lt;/td&gt;&lt;td&gt;12&lt;/td&gt;&lt;td&gt;509&lt;/td&gt;&lt;td&gt;1024&lt;/td&gt;&lt;td&gt;11&lt;/td&gt;&lt;td&gt;4&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;1025&lt;/td&gt;&lt;td&gt;1529&lt;/td&gt;&lt;td&gt;12&lt;/td&gt;&lt;td&gt;504&lt;/td&gt;&lt;td&gt;2048&lt;/td&gt;&lt;td&gt;12&lt;/td&gt;&lt;td&gt;1023&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;1530&lt;/td&gt;&lt;td&gt;2294&lt;/td&gt;&lt;td&gt;13&lt;/td&gt;&lt;td&gt;764&lt;/td&gt;&lt;td&gt;2048&lt;/td&gt;&lt;td&gt;12&lt;/td&gt;&lt;td&gt;518&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;2500&lt;/td&gt;&lt;td&gt;3442&lt;/td&gt;&lt;td&gt;14&lt;/td&gt;&lt;td&gt;942&lt;/td&gt;&lt;td&gt;4096&lt;/td&gt;&lt;td&gt;13&lt;/td&gt;&lt;td&gt;1596&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;3500&lt;/td&gt;&lt;td&gt;5164&lt;/td&gt;&lt;td&gt;15&lt;/td&gt;&lt;td&gt;1664&lt;/td&gt;&lt;td&gt;4096&lt;/td&gt;&lt;td&gt;13&lt;/td&gt;&lt;td&gt;596&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;5500&lt;/td&gt;&lt;td&gt;7747&lt;/td&gt;&lt;td&gt;16&lt;/td&gt;&lt;td&gt;2247&lt;/td&gt;&lt;td&gt;8192&lt;/td&gt;&lt;td&gt;14&lt;/td&gt;&lt;td&gt;2692&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;8000&lt;/td&gt;&lt;td&gt;11621&lt;/td&gt;&lt;td&gt;17&lt;/td&gt;&lt;td&gt;3621&lt;/td&gt;&lt;td&gt;8192&lt;/td&gt;&lt;td&gt;14&lt;/td&gt;&lt;td&gt;192&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;br /&gt;</content><link rel='replies' type='application/atom+xml' href='http://sophie-notes.blogspot.com/feeds/8157536677643390991/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/8742529245277406456/8157536677643390991' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8742529245277406456/posts/default/8157536677643390991'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8742529245277406456/posts/default/8157536677643390991'/><link rel='alternate' type='text/html' href='http://sophie-notes.blogspot.com/2012/12/java-arraylist-capacity-increment-policy.html' title='Java ArrayList Capacity Increment Policy'/><author><name>Sophie</name><uri>http://www.blogger.com/profile/08130938292479634191</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgJYm9W1Baj1dfsHW3J9uikPP0eW2P-JuM4uRrJUZI8b-V2_H1C4tGNKzw_wTUUDzxo6VN9r7LKCHoG4ApDuPcc_X2oqejeV_pL9J1K5ezSnXluTppUj60ZBQQBjt73dA/s220/12.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8742529245277406456.post-5283010672090138795</id><published>2012-12-03T21:39:00.002-08:00</published><updated>2013-03-05T23:37:15.437-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Java"/><category scheme="http://www.blogger.com/atom/ns#" term="Programming"/><title type='text'>Java Passing Arguments into Methods: Pass-by-value(?)</title><content type='html'>After spending half an hour to find out that Java was silently changing my pass-in arguments, I felts like I need to make a note.&lt;br /&gt;
&lt;br /&gt;
If you have been using C++, you must be familiar with pass-by-value and pass-by-reference. How does Java deal with pass-in arguments? Is it pass-by-value? The answer is Yes and No.&lt;br /&gt;
&lt;br /&gt;
Let&#39;s talk it though examples.&lt;br /&gt;
&lt;h4&gt;
Primitive Variables&lt;/h4&gt;
&lt;pre&gt;&lt;code&gt; public static void changeValue1(int d) {
   d = 5;
 }
 public static void main() {
   int a = 3;
   changeValue1(a);
   System.out.println(a); // &lt;b&gt;3&lt;/b&gt;
 }  
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
These two are truly passing by value. That is, any changes to the argument in the methods has no impacts on the actual variable in main.&lt;br /&gt;
&lt;h4&gt;
Arraies&lt;/h4&gt;
&lt;pre&gt;&lt;code&gt; public static void changeValue2(int[] d) {  
   d[1] = 5;
   int[] b = {0,9,8,7};
   d = b;
 }  
 public static void main() {  
   int[] a = {1,2,3,4};
   changeValue2(a);  
   System.out.println(a); // 1,&lt;b&gt;5&lt;/b&gt;,3,4
 }  
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
See the difference? This can be thought as C++&#39;s &lt;i&gt;const pointer.&lt;/i&gt;&amp;nbsp;Java passes in a pointer of the array, make a copy of the pointer into the method, and thus we can change the content but can&#39;t change the pointer itself.&lt;br /&gt;
&lt;h4&gt;
Objects&lt;/h4&gt;
Similarly, Java passes in a pointer of an Object into method rather than the Object itself.&lt;br /&gt;
&lt;pre&gt;&lt;code&gt;
 public void changeValue3(MyClass d) {  
   d.setValue(3);
 }  
 public static void main() {  
   MyClass a = new MyClass();
   a.setValue(5);
   changeValue3(a);  
   System.out.println(a.getValue()); // &lt;b&gt;3&lt;/b&gt;
 }  &lt;/code&gt;&lt;/pre&gt;
&lt;h4&gt;
Strings&lt;/h4&gt;
One thing need to mention is in Java, String is treated as a &lt;b&gt;constant&lt;/b&gt; (c.f. a char array in C++) and thus the content can&#39;t be changed after it is created.&lt;br /&gt;
&lt;pre&gt;&lt;code&gt;
 public void changeValue4(String d) {  
   d.toUpperCase();
 }  
 public static void main() {  
   String a = &quot;abc&quot;;
   changeValue4(a);  
   System.out.println(a); // &lt;b&gt;abc&lt;/b&gt;
 }  
&lt;/code&gt;&lt;/pre&gt;
</content><link rel='replies' type='application/atom+xml' href='http://sophie-notes.blogspot.com/feeds/5283010672090138795/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/8742529245277406456/5283010672090138795' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8742529245277406456/posts/default/5283010672090138795'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8742529245277406456/posts/default/5283010672090138795'/><link rel='alternate' type='text/html' href='http://sophie-notes.blogspot.com/2012/12/java-passing-arguments-into-methods.html' title='Java Passing Arguments into Methods: Pass-by-value(?)'/><author><name>Sophie</name><uri>http://www.blogger.com/profile/08130938292479634191</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgJYm9W1Baj1dfsHW3J9uikPP0eW2P-JuM4uRrJUZI8b-V2_H1C4tGNKzw_wTUUDzxo6VN9r7LKCHoG4ApDuPcc_X2oqejeV_pL9J1K5ezSnXluTppUj60ZBQQBjt73dA/s220/12.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8742529245277406456.post-5877321648752868</id><published>2012-11-13T23:32:00.000-08:00</published><updated>2012-11-13T23:32:19.826-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Linux"/><category scheme="http://www.blogger.com/atom/ns#" term="Programming"/><title type='text'>Using GDB within Emacs</title><content type='html'>I&#39;m lazy and I don&#39;t want to switch back and forth between editor and shell just to debug a c++ program. So here is what I learnt today:&lt;br /&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;i&gt;Emacs has build-in GDB mode since they are from the same family&lt;/i&gt;! :D&lt;/div&gt;
&lt;ol&gt;
&lt;li&gt;Open GDB while editing a c++ file (say, foo.cpp) within Emacs&lt;br /&gt;
&lt;code&gt;&amp;nbsp; M-x gdb&lt;br /&gt;
&amp;nbsp; gdb foo&lt;/code&gt;&lt;br /&gt;
You should expect some version information of GDB like this:&lt;br /&gt;
&lt;code&gt;&amp;nbsp; GNU gdb 6.3.50-20050815 (Apple version gdb-1752) (Sat Jan 28 03:02:46 UTC 2012)&lt;br /&gt;
&amp;nbsp; ... ...&lt;br /&gt;
&amp;nbsp; (gdb)&lt;br /&gt;
&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Run the program and Emacs will load your source code in a second window&lt;br /&gt;
&lt;code&gt;&amp;nbsp; (gdb) run&lt;/code&gt;&lt;br /&gt;
&lt;/li&gt;
&lt;li&gt;Add break points&lt;br /&gt;
In the source code buffer, move to the line you want to pause at and hit&amp;nbsp;&lt;code&gt;C-x Space&lt;/code&gt;&lt;br /&gt;
Alternatively, you can do it in GDB way:&lt;br /&gt;
&lt;code&gt;&amp;nbsp; (gdb) list (or list [start-line, end-line])&lt;br /&gt;
&amp;nbsp; ... ... (source code)&lt;br /&gt;
&amp;nbsp; (gdb) break [line-number][function]&lt;br /&gt;
&amp;nbsp; (gdb) until [line-number][function]&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Debugging with GDB&lt;br /&gt;
&lt;code&gt;&amp;nbsp; (gdb) next (or n)&lt;/code&gt; - run to next line&lt;br /&gt;
&lt;code&gt;&amp;nbsp; (gdb) cont (or c)&lt;/code&gt; - continue to run, will stop at a next break point if exists&lt;br /&gt;
&lt;code&gt;&amp;nbsp; (gdb) step (or s)&lt;/code&gt; - step in&lt;br /&gt;
&lt;code&gt;&amp;nbsp; (gdb) where&lt;/code&gt; - Show a stack trace with source line numbers&lt;br /&gt;
&lt;/li&gt;
&lt;li&gt;Passing in arguments&lt;br /&gt;
&lt;code&gt;&amp;nbsp; (gdb) set args [argv]&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Getting information&lt;br /&gt;
&lt;code&gt;&amp;nbsp; (gdb) print [expression/variable]&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Leave GDB&lt;br /&gt;
&lt;code&gt;&amp;nbsp; (gdb) kill&lt;/code&gt; - Kill the program being debugged&lt;br /&gt;
&lt;code&gt;&amp;nbsp; (gdb) quit&lt;/code&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;div&gt;
Ref:&lt;br /&gt;- An &lt;a href=&quot;http://tedlab.mit.edu/~dr/gdbintro.html&quot; target=&quot;_blank&quot;&gt;introduction&lt;/a&gt;&lt;br /&gt;
- A &lt;a href=&quot;http://www.youtube.com/watch?v=vHOzMOzzxDA&quot; target=&quot;_blank&quot;&gt;Youtube video&lt;/a&gt;&lt;br /&gt;
- Results from &lt;a href=&quot;https://www.google.com/search?q=emacs+gdb&quot; target=&quot;_blank&quot;&gt;Google search&lt;/a&gt;&lt;/div&gt;
&lt;h3&gt;
Vim?&lt;/h3&gt;
&lt;div&gt;
I&#39;m a Vim user. Unfortunately, Vim doesn&#39;t have a build-in tool for GDB. But, luckily, &lt;a href=&quot;http://clewn.sourceforge.net/&quot; target=&quot;_blank&quot;&gt;clewn&lt;/a&gt; has implemented full GDB support in Vim!&lt;br /&gt;
&lt;br /&gt;
Here is a &lt;a href=&quot;http://chunhao.net/blog/how-to-connect-vim-with-gdb-using-clewn&quot; target=&quot;_blank&quot;&gt;step-by-step-how-to&lt;/a&gt;.&lt;/div&gt;
&lt;h3&gt;
Mac?&lt;/h3&gt;
To make it work on Mac, you need to set up some key mappings for Emacs (Thanks to &lt;a href=&quot;http://stackoverflow.com/questions/162896/emacs-on-mac-os-x-leopard-key-bindings&quot; target=&quot;_blank&quot;&gt;Kyle@StackOverflow&lt;/a&gt;):&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;background-color: white; border: 0px; clear: both; font-family: Arial, &#39;Liberation Sans&#39;, &#39;DejaVu Sans&#39;, sans-serif; font-size: 14px; line-height: 18px; margin-bottom: 1em; padding: 0px; vertical-align: baseline; word-wrap: break-word;&quot;&gt;
&lt;strong style=&quot;background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;Using ALT/OPTION as META&lt;/strong&gt;&lt;/div&gt;
&lt;ol style=&quot;background-color: white; border: 0px; font-family: Arial, &#39;Liberation Sans&#39;, &#39;DejaVu Sans&#39;, sans-serif; font-size: 14px; line-height: 18px; list-style-image: initial; list-style-position: initial; margin: 0px 0px 1em 30px; padding: 0px; vertical-align: baseline;&quot;&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline; word-wrap: break-word;&quot;&gt;In the menu bar, click&amp;nbsp;&lt;em style=&quot;background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;Terminal&lt;/em&gt;&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline; word-wrap: break-word;&quot;&gt;Click&amp;nbsp;&lt;em style=&quot;background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;Preferences...&lt;/em&gt;&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline; word-wrap: break-word;&quot;&gt;Under the&amp;nbsp;&lt;em style=&quot;background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;Settings&lt;/em&gt;&amp;nbsp;tab, go to the&amp;nbsp;&lt;em style=&quot;background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;Keyboard&lt;/em&gt;&amp;nbsp;tab&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline; word-wrap: break-word;&quot;&gt;Check the box labeled&amp;nbsp;&lt;em style=&quot;background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;Use option as meta key&lt;/em&gt;&lt;/li&gt;
&lt;/ol&gt;
</content><link rel='replies' type='application/atom+xml' href='http://sophie-notes.blogspot.com/feeds/5877321648752868/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/8742529245277406456/5877321648752868' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8742529245277406456/posts/default/5877321648752868'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8742529245277406456/posts/default/5877321648752868'/><link rel='alternate' type='text/html' href='http://sophie-notes.blogspot.com/2012/11/using-gdb-within-emacs.html' title='Using GDB within Emacs'/><author><name>Sophie</name><uri>http://www.blogger.com/profile/08130938292479634191</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgJYm9W1Baj1dfsHW3J9uikPP0eW2P-JuM4uRrJUZI8b-V2_H1C4tGNKzw_wTUUDzxo6VN9r7LKCHoG4ApDuPcc_X2oqejeV_pL9J1K5ezSnXluTppUj60ZBQQBjt73dA/s220/12.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8742529245277406456.post-4838761277865556879</id><published>2012-11-11T23:30:00.001-08:00</published><updated>2012-11-11T23:41:09.596-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Java"/><category scheme="http://www.blogger.com/atom/ns#" term="Programming"/><title type='text'>Learning Java from Scratch: Java.Util.Collections</title><content type='html'>&lt;a href=&quot;http://docs.oracle.com/javase/6/docs/technotes/guides/collections/index.html&quot; target=&quot;_blank&quot;&gt;Java Collections Framework&lt;/a&gt;&amp;nbsp;provide a set of general-purpose interfaces, implementations and algorithms to manipulate elements in a collection and to operate on a collection. It reduces a lot programming effort by providing methods that can be used in most basic cases. Here is the list of interfaces it has and also general-purpose implementations for each interface in the framework:&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;List&lt;/li&gt;
&lt;ul&gt;
&lt;li&gt;ArrayList - a dynamically resizing array with amortize O(1) access&lt;/li&gt;
&lt;li&gt;LinkedList - a doubly-linked list&lt;/li&gt;
&lt;/ul&gt;
&lt;li&gt;Queue&lt;/li&gt;
&lt;ul&gt;
&lt;li&gt;LinkedList - see above&lt;/li&gt;
&lt;li&gt;PriorityQueue - a priority heap&lt;/li&gt;
&lt;/ul&gt;
&lt;li&gt;Map&lt;/li&gt;
&lt;ul&gt;
&lt;li&gt;HashMap - a hash table that allow null keys/values.&lt;/li&gt;
&lt;li&gt;TreeMap - a red-black tree based sorted map&lt;/li&gt;
&lt;/ul&gt;
&lt;li&gt;Set&lt;/li&gt;
&lt;ul&gt;
&lt;li&gt;HashSet - a set backed by a hash table&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://docs.oracle.com/javase/7/docs/api/java/util/TreeSet.html&quot; target=&quot;_blank&quot;&gt;TreeSet&lt;/a&gt; - a sorted set&lt;/li&gt;
&lt;/ul&gt;
&lt;/ul&gt;
&lt;i&gt;Note that the implementation listed here are not synchronized. To allow multi-thread access, they must be synchronized explicitly.&lt;/i&gt;&lt;br /&gt;
&lt;br /&gt;
I&#39;ll give some basic examples of operations on some of them.&lt;br /&gt;
&lt;h2&gt;
&lt;a href=&quot;http://docs.oracle.com/javase/7/docs/api/java/util/List.html&quot; target=&quot;_blank&quot;&gt;List&amp;lt;E&amp;gt; Interface&lt;/a&gt;&lt;/h2&gt;
List is an ordered collection where users can access elements by index and search for elements. Compared with Set, List allow duplicate elements.
&lt;br /&gt;
&lt;h3&gt;
&lt;a href=&quot;http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html&quot; target=&quot;_blank&quot;&gt;ArrayList&lt;/a&gt;&lt;/h3&gt;
Basic methods:
&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;size, isEmpty, get, set&lt;/code&gt;&amp;nbsp;- O(1)&lt;/li&gt;
&lt;li&gt;&lt;code&gt;add&lt;/code&gt; - amortize O(1)&lt;/li&gt;
&lt;li&gt;&lt;code&gt;contains, indexOf, lastIndexOf, remove&lt;/code&gt; - O(n)&lt;/li&gt;
&lt;/ul&gt;
Example:&lt;br /&gt;
&lt;pre style=&quot;background: #f0f0f0; border: 1px dashed #CCCCCC; color: black; font-family: arial; font-size: 12px; height: 300px; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;&quot;&gt;&lt;code style=&quot;color: black; word-wrap: normal;&quot;&gt;1:  import java.util.ArrayList;  
2:    
3:  public class ArrayListExample {  
4:    public static void printArray(ArrayList&amp;lt;String&amp;gt; array) {  
5:      for (int i=0; i&amp;lt;array.size(); ++i) {  
6:        System.out.println(array.get(i));  
7:      }  
8:    }  
9:    
10:    public static void main(String[] args) {  
11:    
12:      ArrayList&amp;lt;String&amp;gt; myArr = new ArrayList&amp;lt;String&amp;gt;();  
13:      if (myArr.isEmpty())  
14:        System.out.println(&quot;This is an empty array.&quot;);  
15:      myArr.add(&quot;Seattle&quot;);  
16:      myArr.add(&quot;New York&quot;);  
17:      myArr.add(&quot;San Jose&quot;);  
18:      myArr.add(&quot;Las Vegas&quot;);  
19:      myArr.add(&quot;New York&quot;);  
20:      myArr.add(&quot;Long Island&quot;);  
21:      myArr.add(&quot;New York&quot;);  
22:      System.out.println(&quot;The content of the array:&quot;);  
23:      printArray(myArr);  
24:      System.out.println(&quot;Size of the array: &quot; + myArr.size());  
25:    
26:      myArr.set(2, &quot;foo&quot;);  
27:      System.out.println(&quot;The content of the array after setting [2]:&quot;);  
28:      printArray(myArr);  
29:    
30:      myArr.remove(2);  
31:      System.out.println(&quot;The content of the array after removing [2]:&quot;);  
32:      printArray(myArr);  
33:    
34:      myArr.add(2, &quot;San Jose&quot;);  
35:      System.out.println(&quot;The content of the array after adding [2]:&quot;);  
36:      printArray(myArr);  
37:    
38:      String target = &quot;New York&quot;;  
39:      if (myArr.contains(target)) {  
40:        System.out.println(&quot;This array contains &quot; + target);  
41:        System.out.println(&quot;The index of first &quot; + target + &quot; is &quot; + myArr.indexOf(target));  
42:        System.out.println(&quot;The index of last &quot; + target + &quot; is &quot; + myArr.lastIndexOf(target));  
43:    
44:        System.out.println(&quot;Removing the first occurrence of &quot; + target);  
45:        myArr.remove(target);  
46:        System.out.println(&quot;The index of first &quot; + target + &quot; is &quot; + myArr.indexOf(target));  
47:      }  
48:    }  
49:  }  

% java ArrayListExample 
This is an empty array.
The content of the array:
Seattle
New York
San Jose
Las Vegas
New York
Long Island
New York
Size of the array: 7
The content of the array after setting [2]:
Seattle
New York
foo
Las Vegas
New York
Long Island
New York
The content of the array after removing [2]:
Seattle
New York
Las Vegas
New York
Long Island
New York
The content of the array after adding [2]:
Seattle
New York
San Jose
Las Vegas
New York
Long Island
New York
This array contains New York
The index of first New York is 1
The index of last New York is 6
Removing the first occurrence of New York
The index of first New York is 3
&lt;/code&gt;&lt;/pre&gt;
&lt;h3&gt;
&lt;a href=&quot;http://docs.oracle.com/javase/7/docs/api/java/util/LinkedList.html&quot; target=&quot;_blank&quot;&gt;LinkedList&lt;/a&gt;&lt;/h3&gt;
This implementation has the same interface as ArrayList and thus supports all of the above basic methods. &amp;nbsp;In the above example, replace &quot;&lt;code&gt;ArrayList&lt;/code&gt;&quot; with &quot;&lt;code&gt;LinkedList&lt;/code&gt;&quot; and it will return the same results. &amp;nbsp;Note that this time we are using a linked list, the running time may be different. E.g. &lt;code&gt;remove(int index)&lt;/code&gt;, the actual remove action takes time O(1) on a doubly-linked list, but it requires O(n) time to locate the element to be remove.&lt;br /&gt;
&lt;br /&gt;
Basic methods:
&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;size,isEmpty, get()(==getFirst), getLast, add()(==addLast), addFirst, remove(==removeLast), removeFirst&lt;/code&gt;&amp;nbsp;- O(1)&lt;/li&gt;
&lt;li&gt;&lt;code&gt;get(int index), set, contains, indexOf, lastIndexOf, add(int index, E element), remove(int index)&lt;/code&gt; - O(n)&lt;/li&gt;
&lt;/ul&gt;
Queue/Stack methods (these operations access the element at the head of the queue):&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;pop, push,&amp;nbsp;element(peek,&amp;nbsp;peekFirst), peekLast, poll(pollFirst), pollLast&lt;/code&gt;&amp;nbsp;- O(1)&lt;/li&gt;
&lt;/ul&gt;
Example (only for new methods):&lt;br /&gt;
&lt;pre style=&quot;background: #f0f0f0; border: 1px dashed #CCCCCC; color: black; font-family: arial; font-size: 12px; height: 300px; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;&quot;&gt;&lt;code style=&quot;color: black; word-wrap: normal;&quot;&gt;1:  import java.util.Iterator;  
2:  import java.util.LinkedList;  
3:    
4:  public class LinkedListExample {   
5:    public static void printList(LinkedList&amp;lt;String&amp;gt; list) {  
6:      Iterator&amp;lt;String&amp;gt; it = list.iterator();  
7:      while (it.hasNext())  
8:        System.out.println(it.next());  
9:    }  
10:     
11:    public static void main(String[] args) {   
12:     
13:      LinkedList&amp;lt;String&amp;gt; myList = new LinkedList&amp;lt;String&amp;gt;();  
14:      if (myList.isEmpty())  
15:        System.out.println(&quot;This is an empty list.&quot;);  
16:      myList.add(&quot;Seattle&quot;);  
17:      myList.add(&quot;New York&quot;); // equivalent to add(E e)  
18:      myList.add(&quot;San Jose&quot;);  
19:      myList.addFirst(&quot;Las Vegas&quot;);  
20:      myList.addLast(&quot;New York&quot;);  
21:      myList.add(&quot;Long Island&quot;);  
22:      myList.add(&quot;New York&quot;);  
23:      System.out.println(&quot;The content of the list:&quot;);  
24:      printList(myList);  
25:    
26:      String first = myList.peek(); // equivalent to peekFirst()  
27:      System.out.println(&quot;The first of the list is: &quot; + first);  
28:      String last = myList.peekLast();  
29:      System.out.println(&quot;The last of the list is: &quot; + last);  
30:      System.out.println(&quot;The content of the list after peeking:&quot;);  
31:      printList(myList);  
32:    
33:      first = myList.poll(); // equivalent to pollFirst()  
34:      System.out.println(&quot;The first of the list was: &quot; + first);  
35:      last = myList.pollLast();  
36:      System.out.println(&quot;The last of the list was: &quot; + last);  
37:      System.out.println(&quot;The content of the list after polling:&quot;);  
38:      printList(myList);  
39:    
40:      first = myList.removeFirst();  
41:      System.out.println(&quot;The first of the list was: &quot; + first);  
42:      last = myList.removeLast();  
43:      System.out.println(&quot;The last of the list was: &quot; + last);  
44:      System.out.println(&quot;The content of the list after removing:&quot;);  
45:      printList(myList);  
46:     
47:      myList.push(&quot;San Jose&quot;);  
48:      myList.push(first);  
49:      myList.push(last);  
50:      System.out.println(&quot;The content of the list after push:&quot;);  
51:      printList(myList);  
52:    
53:      first = myList.pop();  
54:      System.out.println(&quot;The first of the list was: &quot; + first);  
55:      first = myList.pop();  
56:      System.out.println(&quot;The first of the list was: &quot; + first);  
57:      first = myList.pop();  
58:      System.out.println(&quot;The first of the list was: &quot; + first);  
59:      System.out.println(&quot;The content of the list after pop:&quot;);  
60:      printList(myList);  
61:    }   
62:  }

% java LinkedListExample      
This is an empty list.
The content of the list:
Las Vegas
Seattle
New York
San Jose
New York
Long Island
New York
The first of the list is: Las Vegas
The last of the list is: New York
The content of the list after peeking:
Las Vegas
Seattle
New York
San Jose
New York
Long Island
New York
The first of the list was: Las Vegas
The last of the list was: New York
The content of the list after polling:
Seattle
New York
San Jose
New York
Long Island
The first of the list was: Seattle
The last of the list was: Long Island
The content of the list after removing:
New York
San Jose
New York
The content of the list after push:
Long Island
Seattle
San Jose
New York
San Jose
New York
The first of the list was: Long Island
The first of the list was: Seattle
The first of the list was: San Jose
The content of the list after pop:
New York
San Jose
New York
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
&lt;i&gt;Note: This time we use &lt;code&gt;Iterator&lt;/code&gt; to print out the linked list.
&lt;/i&gt;&lt;br /&gt;
&lt;h2&gt;
&lt;a href=&quot;http://docs.oracle.com/javase/7/docs/api/java/util/Queue.html&quot; target=&quot;_blank&quot;&gt;Queue&amp;lt;E&amp;gt; Interface&lt;/a&gt;&lt;/h2&gt;
There are two implementations for Queue: One is LinkedList which we discussed above; The other is PriorityQueue.&lt;br /&gt;
&lt;h3&gt;
&lt;a href=&quot;http://docs.oracle.com/javase/7/docs/api/java/util/PriorityQueue.html&quot; target=&quot;_blank&quot;&gt;PriorityQueue&lt;/a&gt;&lt;/h3&gt;
A priority queue is based on a priority Min-heap where the head is (one of) the least element(s).&lt;br /&gt;
Basic methods:&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;size, peek, element&lt;/code&gt; - O(1)&lt;/li&gt;
&lt;li&gt;&lt;code&gt;poll, add, remove()&lt;/code&gt; - O(log(n))&lt;/li&gt;
&lt;li&gt;&lt;code&gt;remove(Object), contains(Object)&lt;/code&gt; - O(n)&lt;/li&gt;
&lt;/ul&gt;
Example:&lt;br /&gt;
&lt;pre style=&quot;background: #f0f0f0; border: 1px dashed #CCCCCC; color: black; font-family: arial; font-size: 12px; height: 300px; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;&quot;&gt;&lt;code style=&quot;color: black; word-wrap: normal;&quot;&gt;1:  import java.util.Iterator;  
2:  import java.util.PriorityQueue;  
3:    
4:  public class PriorityQueueExample {   
5:    public static void printList(PriorityQueue&amp;lt;String&amp;gt; list) {  
6:      Iterator&amp;lt;String&amp;gt; it = list.iterator();  
7:      while (it.hasNext())  
8:        System.out.println(it.next());  
9:    }  
10:     
11:    public static void main(String[] args) {   
12:     
13:      PriorityQueue&amp;lt;String&amp;gt; myList = new PriorityQueue&amp;lt;String&amp;gt;();  
14:      if (myList.isEmpty())  
15:        System.out.println(&quot;This is an empty list.&quot;);  
16:      myList.add(&quot;Seattle&quot;);  
17:      myList.add(&quot;New York&quot;); // equivalent to offer(E e)  
18:      myList.add(&quot;San Jose&quot;);  
19:      myList.add(&quot;Las Vegas&quot;);  
20:      myList.add(&quot;New York&quot;);  
21:      myList.add(&quot;Long Island&quot;);  
22:      myList.add(&quot;New York&quot;);  
23:      System.out.println(&quot;The content of the list:&quot;);  
24:      printList(myList);  
25:    
26:      String first = myList.peek();  
27:      System.out.println(&quot;The first of the list is: &quot; + first);  
28:      System.out.println(&quot;The content of the list after peeking:&quot;);  
29:      printList(myList);  
30:    
31:      first = myList.poll();  
32:      System.out.println(&quot;The first of the list was: &quot; + first);  
33:      System.out.println(&quot;The content of the list after polling:&quot;);  
34:      printList(myList);  
35:    
36:      if (!myList.remove(&quot;foo&quot;)) {  
37:        System.out.println(&quot;Failed to remove &#39;foo&#39; since it is not in the list&quot;);  
38:      }  
39:      if (myList.remove(&quot;New York&quot;)) {  
40:        System.out.println(&quot;Removed &#39;New York&#39; from the list&quot;);  
41:      }  
42:      System.out.println(&quot;The content of the list after removing:&quot;);  
43:      printList(myList);  
44:      if (myList.contains(&quot;New York&quot;)) {  
45:        System.out.println(&quot;The list still contains &#39;New York&#39;&quot;);  
46:      }  
47:    }   
48:  }  

% java PriorityQueueExample 
This is an empty list.
The content of the list:
Las Vegas
New York
Long Island
Seattle
New York
San Jose
New York
The first of the list is: Las Vegas
The content of the list after peeking:
Las Vegas
New York
Long Island
Seattle
New York
San Jose
New York
The first of the list was: Las Vegas
The content of the list after polling:
Long Island
New York
New York
Seattle
New York
San Jose
Failed to remove &#39;foo&#39; since it is not in the list
Removed &#39;New York&#39; from the list
The content of the list after removing:
Long Island
New York
New York
Seattle
San Jose
The list still contains &#39;New York&#39;
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
&lt;i&gt;Please notice the difference between &lt;code&gt;Min-heap&lt;/code&gt; and &lt;code&gt;Binary Search Tree&lt;/code&gt;. :-]
&lt;/i&gt;&lt;br /&gt;
&lt;h2&gt;
&lt;a href=&quot;http://docs.oracle.com/javase/7/docs/api/java/util/Map.html&quot; target=&quot;_blank&quot;&gt;Map&amp;lt;K,V&amp;gt;&lt;k&gt;&lt;key value=&quot;value&quot;&gt;&amp;nbsp;Interface&lt;/key&gt;&lt;/k&gt;&lt;/a&gt;&lt;/h2&gt;
A map can map a key to a value. It doesn&#39;t allow duplicate keys and each key can map to at most one value.&lt;br /&gt;
&lt;h3&gt;
&lt;a href=&quot;http://docs.oracle.com/javase/7/docs/api/java/util/HashMap.html&quot; target=&quot;_blank&quot;&gt;HashMap&lt;/a&gt;&lt;/h3&gt;
It is a map; It is a hash table.&lt;br /&gt;
It allows &lt;code&gt;null&lt;/code&gt; values and &lt;code&gt;null&lt;/code&gt; keys. It has no guarantee to the order of the map and makes no guarantee that the order could remain the same over time.&lt;br /&gt;
Basic methods:&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;get, put, containsKey, isEmpty, size, remove&lt;/code&gt; - O(1)&lt;/li&gt;
&lt;li&gt;&lt;code&gt;containsValue, values, keySet, entrySet&lt;/code&gt; - O(n)&lt;/li&gt;
&lt;/ul&gt;
Example:&lt;br /&gt;
&lt;pre style=&quot;background: #f0f0f0; border: 1px dashed #CCCCCC; color: black; font-family: arial; font-size: 12px; height: 300px; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;&quot;&gt;&lt;code style=&quot;color: black; word-wrap: normal;&quot;&gt;1:  import java.util.Map;  
2:  import java.util.HashMap;  
3:    
4:  public class HashMapExample {   
5:    public static void printMap(HashMap&amp;lt;Integer, String&amp;gt; map) {  
6:      for (Map.Entry entry : map.entrySet())  
7:        System.out.println(entry.getKey() + &quot;: &quot; + entry.getValue());  
8:    }  
9:     
10:    public static void main(String[] args) {   
11:     
12:      HashMap&amp;lt;Integer, String&amp;gt; map = new HashMap&amp;lt;Integer, String&amp;gt;();  
13:      if (map.isEmpty())  
14:        System.out.println(&quot;This is an empty map.&quot;);  
15:      map.put(1, &quot;South Lake Union&quot;);  
16:      map.put(2, &quot;BellTown&quot;);  
17:      map.put(3, &quot;Queen Anne&quot;);  
18:      map.put(4, &quot;South Lake Union&quot;);  
19:      map.put(5, &quot;University District&quot;);  
20:      System.out.println(&quot;The content of the map:&quot;);  
21:      printMap(map);  
22:      System.out.println(&quot;The size of the map is &quot; + map.size());  
23:    
24:      String first = map.get(1);  
25:      System.out.println(&quot;The value of key &#39;1&#39; is: &quot; + first);  
26:      first = map.remove(1);  
27:      System.out.println(&quot;The value of key &#39;1&#39; was: &quot; + first);  
28:      System.out.println(&quot;The content of the map after remove:&quot;);  
29:      printMap(map);  
30:    
31:      if (!map.containsKey(1))  
32:        System.out.println(&quot;Key &#39;1&#39; is not in the map any more&quot;);  
33:      if (map.containsValue(&quot;South Lake Union&quot;))  
34:        System.out.println(&quot;Value &#39;South Lake Union&#39; is still in the map&quot;);  
35:    }   
36:  }  

% java HashMapExample    
This is an empty map.
The content of the map:
1: South Lake Union
2: BellTown
3: Queen Anne
4: South Lake Union
5: University District
The size of the map is 5
The value of key &#39;1&#39; is: South Lake Union
The value of key &#39;1&#39; was: South Lake Union
The content of the map after remove:
2: BellTown
3: Queen Anne
4: South Lake Union
5: University District
Key &#39;1&#39; is not in the map any more
Value &#39;South Lake Union&#39; is still in the map
&lt;/code&gt;&lt;/pre&gt;
&lt;h3&gt;
&lt;a href=&quot;http://docs.oracle.com/javase/7/docs/api/java/util/TreeMap.html&quot; target=&quot;_blank&quot;&gt;TreeMap&lt;/a&gt;&lt;/h3&gt;
&lt;div&gt;
It provides guaranteed log(n) time cost for the &lt;code&gt;containsKey, get, put&lt;/code&gt; and &lt;code&gt;remove&lt;/code&gt; operations. As a sorted map, it also provides operations to get ordering information about the map.&lt;/div&gt;
&lt;h2&gt;
&lt;a href=&quot;http://docs.oracle.com/javase/7/docs/api/java/util/Set.html&quot; target=&quot;_blank&quot;&gt;Set&amp;lt;E&amp;gt; Interface&lt;/a&gt;&lt;/h2&gt;
&lt;div&gt;
A set is a collection that doesn&#39;t allow duplicate elements.&lt;/div&gt;
&lt;h3&gt;
&lt;a href=&quot;http://docs.oracle.com/javase/7/docs/api/java/util/HashSet.html&quot; target=&quot;_blank&quot;&gt;HashSet&lt;/a&gt;&lt;/h3&gt;
It is a set backed by a &lt;code&gt;HashMap&lt;/code&gt; instance which offers O(1) time performance for the basic operations (&lt;code&gt;add, remove, contains&lt;/code&gt; and &lt;code&gt;size&lt;/code&gt;).&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;pre style=&quot;background: #f0f0f0; border: 1px dashed #CCCCCC; color: black; font-family: arial; font-size: 12px; height: 300px; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;&quot;&gt;&lt;code style=&quot;color: black; word-wrap: normal;&quot;&gt;1:  import java.util.Iterator;   
2:  import java.util.HashSet;  
3:    
4:  public class HashSetExample {   
5:    public static void printSet(HashSet&amp;lt;String&amp;gt; set) {  
6:      Iterator&amp;lt;String&amp;gt; it = set.iterator();  
7:      while (it.hasNext())  
8:        System.out.println(it.next());  
9:    }  
10:     
11:    public static void main(String[] args) {   
12:     
13:      HashSet&amp;lt;String&amp;gt; set = new HashSet&amp;lt;String&amp;gt;();  
14:      if (set.isEmpty())  
15:        System.out.println(&quot;This is an empty set.&quot;);  
16:      set.add(&quot;South Lake Union&quot;);  
17:      set.add(&quot;BellTown&quot;);  
18:      set.add(&quot;Queen Anne&quot;);  
19:      set.add(&quot;South Lake Union&quot;);  
20:      set.add(&quot;University District&quot;);  
21:      System.out.println(&quot;The content of the set:&quot;);  
22:      printSet(set);  
23:      System.out.println(&quot;The size of the set is &quot; + set.size());  
24:    
25:      if (!set.remove(&quot;foo&quot;))  
26:        System.out.println(&quot;Failed to remove &#39;foo&#39; since not present&quot;);  
27:      if (set.remove(&quot;BellTown&quot;))  
28:        System.out.println(&quot;Succefully removed &#39;BellTown&#39;&quot;);  
29:      System.out.println(&quot;The content of the set after remove:&quot;);  
30:      printSet(set);  
31:    
32:      if (set.contains(&quot;South Lake Union&quot;))  
33:        System.out.println(&quot;Value &#39;South Lake Union&#39; is in the set&quot;);  
34:    }   
35:  }

% java HashSetExample 
This is an empty set.
The content of the set:
University District
BellTown
Queen Anne
South Lake Union
The size of the set is 4
Failed to remove &#39;foo&#39; since not present
Succefully removed &#39;BellTown&#39;
The content of the set after remove:
University District
Queen Anne
South Lake Union
Value &#39;South Lake Union&#39; is in the set
&lt;/code&gt;&lt;/pre&gt;
</content><link rel='replies' type='application/atom+xml' href='http://sophie-notes.blogspot.com/feeds/4838761277865556879/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/8742529245277406456/4838761277865556879' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8742529245277406456/posts/default/4838761277865556879'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8742529245277406456/posts/default/4838761277865556879'/><link rel='alternate' type='text/html' href='http://sophie-notes.blogspot.com/2012/11/learning-java-from-scratch.html' title='Learning Java from Scratch: Java.Util.Collections'/><author><name>Sophie</name><uri>http://www.blogger.com/profile/08130938292479634191</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgJYm9W1Baj1dfsHW3J9uikPP0eW2P-JuM4uRrJUZI8b-V2_H1C4tGNKzw_wTUUDzxo6VN9r7LKCHoG4ApDuPcc_X2oqejeV_pL9J1K5ezSnXluTppUj60ZBQQBjt73dA/s220/12.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8742529245277406456.post-1897635760914024061</id><published>2012-05-10T13:38:00.000-07:00</published><updated>2012-05-10T13:38:41.716-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Interests"/><category scheme="http://www.blogger.com/atom/ns#" term="Linux"/><category scheme="http://www.blogger.com/atom/ns#" term="OS"/><title type='text'>How to clean cached memory on Linux</title><content type='html'>Linux has a suppose-to-be-good strategy for memory management: it caches stuffs into memory and this section of memory would be taken over when any process actually needs it. Thus, sometimes, after downloading a big file truck, you may find that your 8G memory has 16% used by programs but 80% used as cache.&lt;br /&gt;
&lt;br /&gt;
Linux does provide a way to clean up these cached memory and here is a how-to:&lt;br /&gt;
Simply run the following command&lt;br /&gt;
&lt;blockquote class=&quot;tr_bq&quot;&gt;
&lt;code&gt;sync; echo 1 &amp;gt; /proc/sys/vm/drop_caches&lt;/code&gt;
&lt;/blockquote&gt;
&lt;code&gt;sync&lt;/code&gt; is needed to be run in order to make sure all cached objects are freed. Otherwise, dirty objects may not be freeable.&lt;br /&gt;
&lt;code&gt;echo 1&lt;/code&gt; is to free page-cache only; &lt;code&gt;echo 2&lt;/code&gt; is to free dentries and inodes; &lt;code&gt;echo 3&lt;/code&gt; is for all.&lt;br /&gt;
&lt;br /&gt;
&lt;i&gt;Be careful to run this command. It will clean all cached memory, so some programs may run a little slower to get things back to memory.&lt;/i&gt;&lt;br /&gt;
&lt;br /&gt;</content><link rel='replies' type='application/atom+xml' href='http://sophie-notes.blogspot.com/feeds/1897635760914024061/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/8742529245277406456/1897635760914024061' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8742529245277406456/posts/default/1897635760914024061'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8742529245277406456/posts/default/1897635760914024061'/><link rel='alternate' type='text/html' href='http://sophie-notes.blogspot.com/2012/05/how-to-clean-cached-memory-on-linux.html' title='How to clean cached memory on Linux'/><author><name>Sophie</name><uri>http://www.blogger.com/profile/08130938292479634191</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgJYm9W1Baj1dfsHW3J9uikPP0eW2P-JuM4uRrJUZI8b-V2_H1C4tGNKzw_wTUUDzxo6VN9r7LKCHoG4ApDuPcc_X2oqejeV_pL9J1K5ezSnXluTppUj60ZBQQBjt73dA/s220/12.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8742529245277406456.post-5933870207373607898</id><published>2012-04-28T22:02:00.000-07:00</published><updated>2012-04-28T22:02:21.874-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Interests"/><category scheme="http://www.blogger.com/atom/ns#" term="SQL"/><title type='text'>Oracle&#39;s &quot;secret&quot; statistical views (V$ views)</title><content type='html'>You may have use Explain-Plan to evaluate your queries in Oracle. But sometimes you want more information such as what sessions are running, which session takes longest CPU time, etc. Here are two views, &lt;code&gt;V$SQL and V$SQL_PLAN&lt;/code&gt;, and a &lt;code&gt;DBMS_XPLAN&lt;/code&gt; package which are handy in those situations.&lt;br /&gt;
&lt;br /&gt;
PS: I&#39;m not a DBA (yet ?!). So this is just a little knowledge I stole from our DBA when he was helping to tune my queries. Feel free to leave a comment or share your thoughts. Appreciate it! :D&lt;br /&gt;
&lt;h3&gt;

&lt;a href=&quot;http://docs.oracle.com/cd/B28359_01/server.111/b28320/dynviews_3042.htm&quot; target=&quot;_blank&quot;&gt;V$SQL&lt;/a&gt;&lt;/h3&gt;
&lt;blockquote class=&quot;tr_bq&quot;&gt;
&quot;&lt;i&gt;&lt;code&gt;V$SQL&lt;/code&gt; lists statistics on shared SQL areas without the &lt;code&gt;GROUP BY&lt;/code&gt; clause and contains one row for each child of the original SQL text entered.&lt;/i&gt;&quot; (from Oracle 11g Docs)
&lt;/blockquote&gt;
For short running queries, the statistics in this table are updated at the end of query execution; while for long running queries, they are updated every 5 seconds. So, it will be very helpful to see the performance import of long running queries on the fly. Some examples:
&lt;br /&gt;
&lt;blockquote class=&quot;tr_bq&quot;&gt;
&lt;code&gt;select * from v$sql order by LAST_ACTIVE_TIME DESC;&lt;br /&gt;
select SQL_ID, DISK_READS, CPU_TIME, SQL_TEXT from v$sql order by CPU_TIME DESC where SQL_TEXT like &#39;%/* current packages */ from packages%&#39;;&lt;/code&gt;
&lt;/blockquote&gt;
Some useful columns:&lt;br /&gt;
&lt;br /&gt;
&lt;table border=&quot;1&quot; cellpadding=&quot;3&quot; cellspacing=&quot;0&quot; class=&quot;InformalWideMax&quot; dir=&quot;ltr&quot; frame=&quot;hsides&quot; rules=&quot;groups&quot; style=&quot;background-color: white; color: black; font-size: small;&quot; summary=&quot;This table is described in the preceding text&quot; title=&quot;V$SQL view&quot;&gt;&lt;tbody&gt;&lt;/tbody&gt;&lt;thead&gt;
&lt;tr align=&quot;left&quot; valign=&quot;top&quot;&gt;&lt;th align=&quot;left&quot; id=&quot;r1c1-t51&quot; style=&quot;vertical-align: bottom;&quot; valign=&quot;bottom&quot;&gt;Column&lt;/th&gt;&lt;th align=&quot;left&quot; id=&quot;r1c2-t51&quot; style=&quot;vertical-align: bottom;&quot; valign=&quot;bottom&quot;&gt;Datatype&lt;/th&gt;&lt;th align=&quot;left&quot; id=&quot;r1c3-t51&quot; style=&quot;vertical-align: bottom;&quot; valign=&quot;bottom&quot;&gt;Description&lt;/th&gt;&lt;/tr&gt;
&lt;/thead&gt;&lt;tbody&gt;
&lt;tr align=&quot;left&quot; valign=&quot;top&quot;&gt;&lt;td align=&quot;left&quot; headers=&quot;r1c1-t51&quot; id=&quot;r2c1-t51&quot; style=&quot;vertical-align: top;&quot;&gt;&lt;code style=&quot;font-size: 12px;&quot;&gt;SQL_TEXT&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;left&quot; headers=&quot;r2c1-t51 r1c2-t51&quot; style=&quot;vertical-align: top;&quot;&gt;&lt;code style=&quot;font-size: 12px;&quot;&gt;VARCHAR2(1000)&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;left&quot; headers=&quot;r2c1-t51 r1c3-t51&quot; style=&quot;vertical-align: top;&quot;&gt;First thousand characters of the SQL text for the current cursor&lt;/td&gt;&lt;/tr&gt;
&lt;tr align=&quot;left&quot; valign=&quot;top&quot;&gt;&lt;td align=&quot;left&quot; headers=&quot;r1c1-t51&quot; id=&quot;r3c1-t51&quot; style=&quot;vertical-align: top;&quot;&gt;&lt;code style=&quot;font-size: 12px;&quot;&gt;SQL_FULLTEXT&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;left&quot; headers=&quot;r3c1-t51 r1c2-t51&quot; style=&quot;vertical-align: top;&quot;&gt;&lt;code style=&quot;font-size: 12px;&quot;&gt;CLOB&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;left&quot; headers=&quot;r3c1-t51 r1c3-t51&quot; style=&quot;vertical-align: top;&quot;&gt;Full text for the SQL statement exposed as a&amp;nbsp;&lt;code style=&quot;font-size: 12px;&quot;&gt;CLOB&lt;/code&gt;&amp;nbsp;column. The full text of a SQL statement can be retrieved using this column instead of joining with the&lt;code style=&quot;font-size: 12px;&quot;&gt;V$SQLTEXT&lt;/code&gt;&amp;nbsp;dynamic performance view.&lt;/td&gt;&lt;/tr&gt;
&lt;tr align=&quot;left&quot; valign=&quot;top&quot;&gt;&lt;td align=&quot;left&quot; headers=&quot;r1c1-t51&quot; id=&quot;r4c1-t51&quot; style=&quot;vertical-align: top;&quot;&gt;&lt;code style=&quot;font-size: 12px;&quot;&gt;SQL_ID&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;left&quot; headers=&quot;r4c1-t51 r1c2-t51&quot; style=&quot;vertical-align: top;&quot;&gt;&lt;code style=&quot;font-size: 12px;&quot;&gt;VARCHAR2(13)&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;left&quot; headers=&quot;r4c1-t51 r1c3-t51&quot; style=&quot;vertical-align: top;&quot;&gt;SQL identifier of the parent cursor in the library cache&lt;/td&gt;&lt;/tr&gt;
&lt;tr align=&quot;left&quot; valign=&quot;top&quot;&gt;&lt;td align=&quot;left&quot; headers=&quot;r1c1-t51&quot; id=&quot;r7c1-t51&quot; style=&quot;vertical-align: top;&quot;&gt;&lt;code style=&quot;font-size: 12px;&quot;&gt;RUNTIME_MEM&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;left&quot; headers=&quot;r7c1-t51 r1c2-t51&quot; style=&quot;vertical-align: top;&quot;&gt;&lt;code style=&quot;font-size: 12px;&quot;&gt;NUMBER&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;left&quot; headers=&quot;r7c1-t51 r1c3-t51&quot; style=&quot;vertical-align: top;&quot;&gt;Fixed amount of memory required during the execution of the child cursor&lt;/td&gt;&lt;/tr&gt;
&lt;tr align=&quot;left&quot; valign=&quot;top&quot;&gt;&lt;td align=&quot;left&quot; headers=&quot;r1c1-t51&quot; id=&quot;r8c1-t51&quot; style=&quot;vertical-align: top;&quot;&gt;&lt;code style=&quot;font-size: 12px;&quot;&gt;SORTS&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;left&quot; headers=&quot;r8c1-t51 r1c2-t51&quot; style=&quot;vertical-align: top;&quot;&gt;&lt;code style=&quot;font-size: 12px;&quot;&gt;NUMBER&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;left&quot; headers=&quot;r8c1-t51 r1c3-t51&quot; style=&quot;vertical-align: top;&quot;&gt;Number of sorts that were done for the child cursor&lt;/td&gt;&lt;/tr&gt;
&lt;tr align=&quot;left&quot; valign=&quot;top&quot;&gt;&lt;td align=&quot;left&quot; headers=&quot;r1c1-t51&quot; id=&quot;r12c1-t51&quot; style=&quot;vertical-align: top;&quot;&gt;&lt;code style=&quot;font-size: 12px;&quot;&gt;FETCHES&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;left&quot; headers=&quot;r12c1-t51 r1c2-t51&quot; style=&quot;vertical-align: top;&quot;&gt;&lt;code style=&quot;font-size: 12px;&quot;&gt;NUMBER&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;left&quot; headers=&quot;r12c1-t51 r1c3-t51&quot; style=&quot;vertical-align: top;&quot;&gt;Number of fetches associated with the SQL statement&lt;/td&gt;&lt;/tr&gt;
&lt;tr align=&quot;left&quot; valign=&quot;top&quot;&gt;&lt;td align=&quot;left&quot; headers=&quot;r1c1-t51&quot; id=&quot;r13c1-t51&quot; style=&quot;vertical-align: top;&quot;&gt;&lt;code style=&quot;font-size: 12px;&quot;&gt;EXECUTIONS&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;left&quot; headers=&quot;r13c1-t51 r1c2-t51&quot; style=&quot;vertical-align: top;&quot;&gt;&lt;code style=&quot;font-size: 12px;&quot;&gt;NUMBER&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;left&quot; headers=&quot;r13c1-t51 r1c3-t51&quot; style=&quot;vertical-align: top;&quot;&gt;Number of executions that took place on this object since it was brought into the library cache&lt;/td&gt;&lt;/tr&gt;
&lt;tr align=&quot;left&quot; valign=&quot;top&quot;&gt;&lt;td align=&quot;left&quot; headers=&quot;r1c1-t51&quot; id=&quot;r21c1-t51&quot; style=&quot;vertical-align: top;&quot;&gt;&lt;code style=&quot;font-size: 12px;&quot;&gt;DISK_READS&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;left&quot; headers=&quot;r21c1-t51 r1c2-t51&quot; style=&quot;vertical-align: top;&quot;&gt;&lt;code style=&quot;font-size: 12px;&quot;&gt;NUMBER&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;left&quot; headers=&quot;r21c1-t51 r1c3-t51&quot; style=&quot;vertical-align: top;&quot;&gt;Number of disk reads for this child cursor&lt;/td&gt;&lt;/tr&gt;
&lt;tr align=&quot;left&quot; valign=&quot;top&quot;&gt;&lt;td align=&quot;left&quot; headers=&quot;r1c1-t51&quot; id=&quot;r22c1-t51&quot; style=&quot;vertical-align: top;&quot;&gt;&lt;code style=&quot;font-size: 12px;&quot;&gt;DIRECT_WRITES&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;left&quot; headers=&quot;r22c1-t51 r1c2-t51&quot; style=&quot;vertical-align: top;&quot;&gt;&lt;code style=&quot;font-size: 12px;&quot;&gt;NUMBER&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;left&quot; headers=&quot;r22c1-t51 r1c3-t51&quot; style=&quot;vertical-align: top;&quot;&gt;Number of direct writes for this child cursor&lt;/td&gt;&lt;/tr&gt;
&lt;tr align=&quot;left&quot; valign=&quot;top&quot;&gt;&lt;td align=&quot;left&quot; headers=&quot;r1c1-t51&quot; id=&quot;r28c1-t51&quot; style=&quot;vertical-align: top;&quot;&gt;&lt;code style=&quot;font-size: 12px;&quot;&gt;PLSQL_EXEC_TIME&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;left&quot; headers=&quot;r28c1-t51 r1c2-t51&quot; style=&quot;vertical-align: top;&quot;&gt;&lt;code style=&quot;font-size: 12px;&quot;&gt;NUMBER&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;left&quot; headers=&quot;r28c1-t51 r1c3-t51&quot; style=&quot;vertical-align: top;&quot;&gt;PL/SQL execution time (in microseconds)&lt;/td&gt;&lt;/tr&gt;
&lt;tr align=&quot;left&quot; valign=&quot;top&quot;&gt;&lt;td align=&quot;left&quot; headers=&quot;r1c1-t51&quot; id=&quot;r29c1-t51&quot; style=&quot;vertical-align: top;&quot;&gt;&lt;code style=&quot;font-size: 12px;&quot;&gt;JAVA_EXEC_TIME&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;left&quot; headers=&quot;r29c1-t51 r1c2-t51&quot; style=&quot;vertical-align: top;&quot;&gt;&lt;code style=&quot;font-size: 12px;&quot;&gt;NUMBER&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;left&quot; headers=&quot;r29c1-t51 r1c3-t51&quot; style=&quot;vertical-align: top;&quot;&gt;Java execution time (in microseconds)&lt;/td&gt;&lt;/tr&gt;
&lt;tr align=&quot;left&quot; valign=&quot;top&quot;&gt;&lt;td align=&quot;left&quot; headers=&quot;r1c1-t51&quot; id=&quot;r30c1-t51&quot; style=&quot;vertical-align: top;&quot;&gt;&lt;code style=&quot;font-size: 12px;&quot;&gt;ROWS_PROCESSED&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;left&quot; headers=&quot;r30c1-t51 r1c2-t51&quot; style=&quot;vertical-align: top;&quot;&gt;&lt;code style=&quot;font-size: 12px;&quot;&gt;NUMBER&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;left&quot; headers=&quot;r30c1-t51 r1c3-t51&quot; style=&quot;vertical-align: top;&quot;&gt;Total number of rows the parsed SQL statement returns&lt;/td&gt;&lt;/tr&gt;
&lt;tr align=&quot;left&quot; valign=&quot;top&quot;&gt;&lt;td align=&quot;left&quot; headers=&quot;r1c1-t51&quot; id=&quot;r54c1-t51&quot; style=&quot;vertical-align: top;&quot;&gt;&lt;code style=&quot;font-size: 12px;&quot;&gt;CPU_TIME&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;left&quot; headers=&quot;r54c1-t51 r1c2-t51&quot; style=&quot;vertical-align: top;&quot;&gt;&lt;code style=&quot;font-size: 12px;&quot;&gt;NUMBER&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;left&quot; headers=&quot;r54c1-t51 r1c3-t51&quot; style=&quot;vertical-align: top;&quot;&gt;CPU time (in microseconds) used by this cursor for parsing, executing, and fetching&lt;/td&gt;&lt;/tr&gt;
&lt;tr align=&quot;left&quot; valign=&quot;top&quot;&gt;&lt;td align=&quot;left&quot; headers=&quot;r1c1-t51&quot; id=&quot;r55c1-t51&quot; style=&quot;vertical-align: top;&quot;&gt;&lt;code style=&quot;font-size: 12px;&quot;&gt;ELAPSED_TIME&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;left&quot; headers=&quot;r55c1-t51 r1c2-t51&quot; style=&quot;vertical-align: top;&quot;&gt;&lt;code style=&quot;font-size: 12px;&quot;&gt;NUMBER&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;left&quot; headers=&quot;r55c1-t51 r1c3-t51&quot; style=&quot;vertical-align: top;&quot;&gt;Elapsed time (in microseconds) used by this cursor for parsing, executing, and fetching&lt;/td&gt;&lt;/tr&gt;
&lt;tr align=&quot;left&quot; valign=&quot;top&quot;&gt;&lt;td align=&quot;left&quot; headers=&quot;r1c1-t51&quot; id=&quot;r62c1-t51&quot; style=&quot;vertical-align: top;&quot;&gt;&lt;code style=&quot;font-size: 12px;&quot;&gt;LAST_LOAD_TIME&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;left&quot; headers=&quot;r62c1-t51 r1c2-t51&quot; style=&quot;vertical-align: top;&quot;&gt;&lt;code style=&quot;font-size: 12px;&quot;&gt;VARCHAR2(19)&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;left&quot; headers=&quot;r62c1-t51 r1c3-t51&quot; style=&quot;vertical-align: top;&quot;&gt;Time at which the query plan was loaded into the library cache&lt;/td&gt;&lt;/tr&gt;
&lt;tr align=&quot;left&quot; valign=&quot;top&quot;&gt;&lt;td align=&quot;left&quot; headers=&quot;r1c1-t51&quot; id=&quot;r75c1-t51&quot; style=&quot;vertical-align: top;&quot;&gt;&lt;code style=&quot;font-size: 12px;&quot;&gt;LAST_ACTIVE_TIME&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;left&quot; headers=&quot;r75c1-t51 r1c2-t51&quot; style=&quot;vertical-align: top;&quot;&gt;&lt;code style=&quot;font-size: 12px;&quot;&gt;DATE&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;left&quot; headers=&quot;r75c1-t51 r1c3-t51&quot; style=&quot;vertical-align: top;&quot;&gt;Time at which the query plan was last active&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;h3&gt;

&lt;a href=&quot;http://docs.oracle.com/cd/B28359_01/server.111/b28320/dynviews_3050.htm&quot; target=&quot;_blank&quot;&gt;V$SQL_PLAN&lt;/a&gt;&lt;/h3&gt;
This table contains execution plan information and may be the major view behind explain-plan.&lt;br /&gt;
Some useful columns:&lt;br /&gt;
&lt;br /&gt;
&lt;table border=&quot;1&quot; cellpadding=&quot;3&quot; cellspacing=&quot;0&quot; class=&quot;InformalWideMax&quot; dir=&quot;ltr&quot; frame=&quot;hsides&quot; rules=&quot;groups&quot; style=&quot;font-size: small;&quot; summary=&quot;This table is described in the preceding text&quot; title=&quot;V$SQL_PLAN view&quot;&gt;&lt;colgroup&gt;&lt;col width=&quot;24%&quot;&gt;&lt;/col&gt;&lt;col width=&quot;18%&quot;&gt;&lt;/col&gt;&lt;col width=&quot;*&quot;&gt;&lt;/col&gt;&lt;/colgroup&gt;&lt;thead&gt;
&lt;tr align=&quot;left&quot; valign=&quot;top&quot;&gt;&lt;th align=&quot;left&quot; id=&quot;r1c1-t59&quot; style=&quot;vertical-align: bottom;&quot; valign=&quot;bottom&quot;&gt;Column&lt;/th&gt;&lt;th align=&quot;left&quot; id=&quot;r1c2-t59&quot; style=&quot;vertical-align: bottom;&quot; valign=&quot;bottom&quot;&gt;Datatype&lt;/th&gt;&lt;th align=&quot;left&quot; id=&quot;r1c3-t59&quot; style=&quot;vertical-align: bottom;&quot; valign=&quot;bottom&quot;&gt;Description&lt;/th&gt;&lt;/tr&gt;
&lt;/thead&gt;&lt;tbody&gt;
&lt;tr align=&quot;left&quot; valign=&quot;top&quot;&gt;&lt;td align=&quot;left&quot; headers=&quot;r1c1-t59&quot; id=&quot;r2c1-t59&quot; style=&quot;vertical-align: top;&quot;&gt;&lt;code style=&quot;font-size: 12px;&quot;&gt;ADDRESS&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;left&quot; headers=&quot;r2c1-t59 r1c2-t59&quot; style=&quot;vertical-align: top;&quot;&gt;&lt;code style=&quot;font-size: 12px;&quot;&gt;RAW(4 | 8)&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;left&quot; headers=&quot;r2c1-t59 r1c3-t59&quot; style=&quot;vertical-align: top;&quot;&gt;Address of the handle to the parent for this cursor&lt;/td&gt;&lt;/tr&gt;
&lt;tr align=&quot;left&quot; valign=&quot;top&quot;&gt;&lt;td align=&quot;left&quot; headers=&quot;r1c1-t59&quot; id=&quot;r3c1-t59&quot; style=&quot;vertical-align: top;&quot;&gt;&lt;code style=&quot;font-size: 12px;&quot;&gt;HASH_VALUE&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;left&quot; headers=&quot;r3c1-t59 r1c2-t59&quot; style=&quot;vertical-align: top;&quot;&gt;&lt;code style=&quot;font-size: 12px;&quot;&gt;NUMBER&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;left&quot; headers=&quot;r3c1-t59 r1c3-t59&quot; style=&quot;vertical-align: top;&quot;&gt;Hash value of the parent statement in the library cache. The two columns &lt;code style=&quot;font-size: 12px;&quot;&gt;ADDRESS&lt;/code&gt; and &lt;code style=&quot;font-size: 12px;&quot;&gt;HASH_VALUE&lt;/code&gt; can be used to join with&amp;nbsp;&lt;code style=&quot;font-size: 12px;&quot;&gt;V$SQLAREA&lt;/code&gt; to add the cursor-specific information.&lt;/td&gt;&lt;/tr&gt;
&lt;tr align=&quot;left&quot; valign=&quot;top&quot;&gt;&lt;td align=&quot;left&quot; headers=&quot;r1c1-t59&quot; id=&quot;r4c1-t59&quot; style=&quot;vertical-align: top;&quot;&gt;&lt;code style=&quot;font-size: 12px;&quot;&gt;SQL_ID&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;left&quot; headers=&quot;r4c1-t59 r1c2-t59&quot; style=&quot;vertical-align: top;&quot;&gt;&lt;code style=&quot;font-size: 12px;&quot;&gt;VARCHAR2(13)&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;left&quot; headers=&quot;r4c1-t59 r1c3-t59&quot; style=&quot;vertical-align: top;&quot;&gt;SQL identifier of the parent cursor in the library cache&lt;/td&gt;&lt;/tr&gt;
&lt;tr align=&quot;left&quot; valign=&quot;top&quot;&gt;&lt;td align=&quot;left&quot; headers=&quot;r1c1-t59&quot; id=&quot;r8c1-t59&quot; style=&quot;vertical-align: top;&quot;&gt;&lt;code style=&quot;font-size: 12px;&quot;&gt;TIMESTAMP&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;left&quot; headers=&quot;r8c1-t59 r1c2-t59&quot; style=&quot;vertical-align: top;&quot;&gt;&lt;code style=&quot;font-size: 12px;&quot;&gt;DATE&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;left&quot; headers=&quot;r8c1-t59 r1c3-t59&quot; style=&quot;vertical-align: top;&quot;&gt;Date and time when the execution plan was generated&lt;/td&gt;&lt;/tr&gt;
&lt;tr align=&quot;left&quot; valign=&quot;top&quot;&gt;&lt;td align=&quot;left&quot; headers=&quot;r1c1-t59&quot; id=&quot;r9c1-t59&quot; style=&quot;vertical-align: top;&quot;&gt;&lt;code style=&quot;font-size: 12px;&quot;&gt;OPERATION&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;left&quot; headers=&quot;r9c1-t59 r1c2-t59&quot; style=&quot;vertical-align: top;&quot;&gt;&lt;code style=&quot;font-size: 12px;&quot;&gt;VARCHAR2(30)&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;left&quot; headers=&quot;r9c1-t59 r1c3-t59&quot; style=&quot;vertical-align: top;&quot;&gt;Name of the internal operation performed in this step (for example,&amp;nbsp;&lt;code style=&quot;font-size: 12px;&quot;&gt;TABLE ACCESS&lt;/code&gt;)&lt;/td&gt;&lt;/tr&gt;
&lt;tr align=&quot;left&quot; valign=&quot;top&quot;&gt;&lt;td align=&quot;left&quot; headers=&quot;r1c1-t59&quot; id=&quot;r17c1-t59&quot; style=&quot;vertical-align: top;&quot;&gt;&lt;code style=&quot;font-size: 12px;&quot;&gt;OPTIMIZER&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;left&quot; headers=&quot;r17c1-t59 r1c2-t59&quot; style=&quot;vertical-align: top;&quot;&gt;&lt;code style=&quot;font-size: 12px;&quot;&gt;VARCHAR2(20)&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;left&quot; headers=&quot;r17c1-t59 r1c3-t59&quot; style=&quot;vertical-align: top;&quot;&gt;Current mode of the optimizer for the first row in the plan (statement line), for example,&amp;nbsp;&lt;code style=&quot;font-size: 12px;&quot;&gt;CHOOSE&lt;/code&gt;. When the operation is a database access (for example,&amp;nbsp;&lt;code style=&quot;font-size: 12px;&quot;&gt;TABLE ACCESS&lt;/code&gt;), this column indicates whether or not the object is analyzed.&lt;/td&gt;&lt;/tr&gt;
&lt;tr align=&quot;left&quot; valign=&quot;top&quot;&gt;&lt;td align=&quot;left&quot; headers=&quot;r1c1-t59&quot; id=&quot;r18c1-t59&quot; style=&quot;vertical-align: top;&quot;&gt;&lt;code style=&quot;font-size: 12px;&quot;&gt;ID&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;left&quot; headers=&quot;r18c1-t59 r1c2-t59&quot; style=&quot;vertical-align: top;&quot;&gt;&lt;code style=&quot;font-size: 12px;&quot;&gt;NUMBER&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;left&quot; headers=&quot;r18c1-t59 r1c3-t59&quot; style=&quot;vertical-align: top;&quot;&gt;A number assigned to each step in the execution plan&lt;/td&gt;&lt;/tr&gt;
&lt;tr align=&quot;left&quot; valign=&quot;top&quot;&gt;&lt;td align=&quot;left&quot; headers=&quot;r1c1-t59&quot; id=&quot;r20c1-t59&quot; style=&quot;vertical-align: top;&quot;&gt;&lt;code style=&quot;font-size: 12px;&quot;&gt;DEPTH&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;left&quot; headers=&quot;r20c1-t59 r1c2-t59&quot; style=&quot;vertical-align: top;&quot;&gt;&lt;code style=&quot;font-size: 12px;&quot;&gt;NUMBER&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;left&quot; headers=&quot;r20c1-t59 r1c3-t59&quot; style=&quot;vertical-align: top;&quot;&gt;Depth (or level) of the operation in the tree. It is not necessary to issue a&lt;code style=&quot;font-size: 12px;&quot;&gt;CONNECT BY&lt;/code&gt;&amp;nbsp;statement to get the level information, which is generally used to indent the rows from the&amp;nbsp;&lt;code style=&quot;font-size: 12px;&quot;&gt;PLAN_TABLE&lt;/code&gt;&amp;nbsp;table. The root operation (statement) is level 0.&lt;/td&gt;&lt;/tr&gt;
&lt;tr align=&quot;left&quot; valign=&quot;top&quot;&gt;&lt;td align=&quot;left&quot; headers=&quot;r1c1-t59&quot; id=&quot;r23c1-t59&quot; style=&quot;vertical-align: top;&quot;&gt;&lt;code style=&quot;font-size: 12px;&quot;&gt;COST&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;left&quot; headers=&quot;r23c1-t59 r1c2-t59&quot; style=&quot;vertical-align: top;&quot;&gt;&lt;code style=&quot;font-size: 12px;&quot;&gt;NUMBER&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;left&quot; headers=&quot;r23c1-t59 r1c3-t59&quot; style=&quot;vertical-align: top;&quot;&gt;Cost of the operation as estimated by the optimizer&#39;s cost-based approach. For statements that use the rule-based approach, this column is null.&lt;/td&gt;&lt;/tr&gt;
&lt;tr align=&quot;left&quot; valign=&quot;top&quot;&gt;&lt;td align=&quot;left&quot; headers=&quot;r1c1-t59&quot; id=&quot;r24c1-t59&quot; style=&quot;vertical-align: top;&quot;&gt;&lt;code style=&quot;font-size: 12px;&quot;&gt;CARDINALITY&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;left&quot; headers=&quot;r24c1-t59 r1c2-t59&quot; style=&quot;vertical-align: top;&quot;&gt;&lt;code style=&quot;font-size: 12px;&quot;&gt;NUMBER&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;left&quot; headers=&quot;r24c1-t59 r1c3-t59&quot; style=&quot;vertical-align: top;&quot;&gt;Estimate, by the cost-based optimizer, of the number of rows produced by the operation&lt;/td&gt;&lt;/tr&gt;
&lt;tr align=&quot;left&quot; valign=&quot;top&quot;&gt;&lt;td align=&quot;left&quot; headers=&quot;r1c1-t59&quot; id=&quot;r25c1-t59&quot; style=&quot;vertical-align: top;&quot;&gt;&lt;code style=&quot;font-size: 12px;&quot;&gt;BYTES&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;left&quot; headers=&quot;r25c1-t59 r1c2-t59&quot; style=&quot;vertical-align: top;&quot;&gt;&lt;code style=&quot;font-size: 12px;&quot;&gt;NUMBER&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;left&quot; headers=&quot;r25c1-t59 r1c3-t59&quot; style=&quot;vertical-align: top;&quot;&gt;Estimate, by the cost-based optimizer, of the number of bytes produced by the operation&lt;/td&gt;&lt;/tr&gt;
&lt;tr align=&quot;left&quot; valign=&quot;top&quot;&gt;&lt;td align=&quot;left&quot; headers=&quot;r1c1-t59&quot; id=&quot;r32c1-t59&quot; style=&quot;vertical-align: top;&quot;&gt;&lt;code style=&quot;font-size: 12px;&quot;&gt;CPU_COST&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;left&quot; headers=&quot;r32c1-t59 r1c2-t59&quot; style=&quot;vertical-align: top;&quot;&gt;&lt;code style=&quot;font-size: 12px;&quot;&gt;NUMBER&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;left&quot; headers=&quot;r32c1-t59 r1c3-t59&quot; style=&quot;vertical-align: top;&quot;&gt;CPU cost of the operation as estimated by the optimizer&#39;s cost-based approach. For statements that use the rule-based approach, this column is null.&lt;/td&gt;&lt;/tr&gt;
&lt;tr align=&quot;left&quot; valign=&quot;top&quot;&gt;&lt;td align=&quot;left&quot; headers=&quot;r1c1-t59&quot; id=&quot;r33c1-t59&quot; style=&quot;vertical-align: top;&quot;&gt;&lt;code style=&quot;font-size: 12px;&quot;&gt;IO_COST&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;left&quot; headers=&quot;r33c1-t59 r1c2-t59&quot; style=&quot;vertical-align: top;&quot;&gt;&lt;code style=&quot;font-size: 12px;&quot;&gt;NUMBER&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;left&quot; headers=&quot;r33c1-t59 r1c3-t59&quot; style=&quot;vertical-align: top;&quot;&gt;I/O cost of the operation as estimated by the optimizer&#39;s cost-based approach. For statements that use the rule-based approach, this column is null.&lt;/td&gt;&lt;/tr&gt;
&lt;tr align=&quot;left&quot; valign=&quot;top&quot;&gt;&lt;td align=&quot;left&quot; headers=&quot;r1c1-t59&quot; id=&quot;r34c1-t59&quot; style=&quot;vertical-align: top;&quot;&gt;&lt;code style=&quot;font-size: 12px;&quot;&gt;TEMP_SPACE&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;left&quot; headers=&quot;r34c1-t59 r1c2-t59&quot; style=&quot;vertical-align: top;&quot;&gt;&lt;code style=&quot;font-size: 12px;&quot;&gt;NUMBER&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;left&quot; headers=&quot;r34c1-t59 r1c3-t59&quot; style=&quot;vertical-align: top;&quot;&gt;Temporary space usage of the operation (sort or hash-join) as estimated by the optimizer&#39;s cost-based approach. For statements that use the rule-based approach, this column is null.&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;h3&gt;

&lt;a href=&quot;http://docs.oracle.com/cd/B28359_01/appdev.111/b28419/d_xplan.htm#i999234&quot; target=&quot;_blank&quot;&gt;DBMS_XPLAN&lt;/a&gt;&lt;/h3&gt;
&lt;blockquote class=&quot;tr_bq&quot;&gt;
&quot;&lt;i&gt;The &lt;code&gt;DBMS_XPLAN&lt;/code&gt; package provides an easy way to display the output of the &lt;code&gt;EXPLAIN PLAN&lt;/code&gt; command in several, predefined formats.&quot;&lt;br /&gt;
&quot;It further provides a way to display the SQL execution plan and SQL execution runtime statistics for cached SQL cursors based on the information stored in the &lt;code&gt;V$SQL_PLAN&lt;/code&gt; and &lt;code&gt;V$SQL_PLAN_STATISTICS_ALL&lt;/code&gt; fixed views. Finally, it displays plans from a SQL plan baseline.&lt;/i&gt;&quot; (from Oracle 11g Docs) 
&lt;/blockquote&gt;
This package provides five functions:
&lt;br /&gt;
&lt;br /&gt;
&lt;table border=&quot;1&quot; cellpadding=&quot;2&quot; cellspacing=&quot;0&quot; class=&quot;InformalWideMax&quot; dir=&quot;ltr&quot; frame=&quot;hsides&quot; rules=&quot;groups&quot; style=&quot;font-size: small;&quot; summary=&quot;This table is described in the preceding text&quot; title=&quot;DBMS_XPLAN package&quot;&gt;&lt;colgroup&gt;&lt;col width=&quot;40%&quot;&gt;&lt;/col&gt;&lt;col width=&quot;*&quot;&gt;&lt;/col&gt;&lt;/colgroup&gt;&lt;thead&gt;
&lt;tr align=&quot;left&quot; valign=&quot;top&quot;&gt;
&lt;th align=&quot;left&quot; id=&quot;r1c1-t59&quot; style=&quot;vertical-align: bottom;&quot; valign=&quot;bottom&quot;&gt;Function&lt;/th&gt;&lt;th align=&quot;left&quot; id=&quot;r1c2-t59&quot; style=&quot;vertical-align: bottom;&quot; valign=&quot;bottom&quot;&gt;Description&lt;/th&gt;&lt;/tr&gt;
&lt;/thead&gt;&lt;tbody&gt;
&lt;tr align=&quot;left&quot; valign=&quot;top&quot;&gt;&lt;td align=&quot;left&quot; headers=&quot;r1c11t59&quot; id=&quot;r2c1-t51&quot; style=&quot;vertical-align: top;&quot;&gt;&lt;code style=&quot;font-size: 12px;&quot;&gt;DISPLAY&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;left&quot; headers=&quot;r2c1-t51 r1c2-t51&quot; style=&quot;vertical-align: top;&quot;&gt;Format and display the contents of a plan table&lt;/td&gt;&lt;/tr&gt;
&lt;tr align=&quot;left&quot; valign=&quot;top&quot;&gt;&lt;td align=&quot;left&quot; headers=&quot;r1c1-t51&quot; id=&quot;r3c1-t51&quot; style=&quot;vertical-align: top;&quot;&gt;&lt;code style=&quot;font-size: 12px;&quot;&gt;DISPLAY_SQL_PLAN_BASELINE&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;left&quot; headers=&quot;r3c1-t51 r1c2-t51&quot; style=&quot;vertical-align: top;&quot;&gt;Display one or more execution plans for the SQL statement identified by SQL handle&lt;/td&gt;&lt;/tr&gt;
&lt;tr align=&quot;left&quot; valign=&quot;top&quot;&gt;&lt;td align=&quot;left&quot; headers=&quot;r1c1-t51&quot; id=&quot;r4c1-t51&quot; style=&quot;vertical-align: top;&quot;&gt;&lt;code style=&quot;font-size: 12px;&quot;&gt;DISPLAY_SQLSET&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;left&quot; headers=&quot;r4c1-t51 r1c2-t51&quot; style=&quot;vertical-align: top;&quot;&gt;Format and display the contents of the execution plan of statements stored in a SQL tuning set.&lt;/td&gt;&lt;/tr&gt;
&lt;tr align=&quot;left&quot; valign=&quot;top&quot;&gt;&lt;td align=&quot;left&quot; headers=&quot;r1c1-t51&quot; id=&quot;r5c1-t51&quot; style=&quot;vertical-align: top;&quot;&gt;&lt;code style=&quot;font-size: 12px;&quot;&gt;DISPLAY_CURSOR&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;left&quot; headers=&quot;r5c1-t51 r1c2-t51&quot; style=&quot;vertical-align: top;&quot;&gt;Format and display the contents of the execution plan of any loaded cursor.&lt;/td&gt;&lt;/tr&gt;
&lt;tr align=&quot;left&quot; valign=&quot;top&quot;&gt;&lt;td align=&quot;left&quot; headers=&quot;r1c1-t51&quot; id=&quot;r6c1-t51&quot; style=&quot;vertical-align: top;&quot;&gt;&lt;code style=&quot;font-size: 12px;&quot;&gt;DISPLAY_AWR&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;left&quot; headers=&quot;r6c1-t51 r1c2-t51&quot; style=&quot;vertical-align: top;&quot;&gt;Format and display the contents of the execution plan of a stored SQL statement in the Automatic Workload Repository (AWR).&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;br /&gt;
Examples and function usages can be found &lt;a href=&quot;http://docs.oracle.com/cd/B28359_01/appdev.111/b28419/d_xplan.htm#i999234&quot; target=&quot;_blank&quot;&gt;here&lt;/a&gt;.</content><link rel='replies' type='application/atom+xml' href='http://sophie-notes.blogspot.com/feeds/5933870207373607898/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/8742529245277406456/5933870207373607898' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8742529245277406456/posts/default/5933870207373607898'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8742529245277406456/posts/default/5933870207373607898'/><link rel='alternate' type='text/html' href='http://sophie-notes.blogspot.com/2012/04/oracles-secret-statistical-views-v.html' title='Oracle&#39;s &quot;secret&quot; statistical views (V$ views)'/><author><name>Sophie</name><uri>http://www.blogger.com/profile/08130938292479634191</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgJYm9W1Baj1dfsHW3J9uikPP0eW2P-JuM4uRrJUZI8b-V2_H1C4tGNKzw_wTUUDzxo6VN9r7LKCHoG4ApDuPcc_X2oqejeV_pL9J1K5ezSnXluTppUj60ZBQQBjt73dA/s220/12.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8742529245277406456.post-598972915432085970</id><published>2012-04-17T23:44:00.000-07:00</published><updated>2012-04-17T23:44:31.894-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Perl"/><category scheme="http://www.blogger.com/atom/ns#" term="Programming"/><title type='text'>Variable scoping and subroutines in Perl</title><content type='html'>First of all, Perl contains built in documentation via the &lt;a href=&quot;http://perldoc.perl.org/perldoc.html&quot; target=&quot;_blank&quot;&gt;perldoc&lt;/a&gt; command!&lt;br /&gt;
&lt;br /&gt;
Now back to the topic:&amp;nbsp;&lt;u&gt;Variable Scoping&lt;/u&gt;&lt;br /&gt;
&lt;br /&gt;
As programmers, we&#39;ve heard dozens of time about variable scoping, no matter you are using C++, Java, or Perl. But if you are like me, playing with C++/Java for years but suddenly need to try Perl, this small note and this &lt;a href=&quot;http://perldoc.perl.org/perlsub.html&quot; target=&quot;_blank&quot;&gt;perlsub tutorial&lt;/a&gt;&amp;nbsp;may be useful for you too.&lt;br /&gt;
&lt;br /&gt;
Basically, there are &lt;strike&gt;two&lt;/strike&gt;&amp;nbsp;three types of variables:&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;private (i.e. lexically local) variables via &lt;code&gt;my $variable;&lt;/code&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;[1]&lt;/span&gt; which is visible to the enclosing block.&lt;/li&gt;
&lt;li&gt;global package variables via &lt;code&gt;our $variable;&lt;/code&gt; or &lt;code&gt;use $variable;&lt;/code&gt; which is visible to the entire file.&lt;/li&gt;
&lt;li&gt;persistant private (like C static) variables via &lt;code&gt;state $variable;&lt;/code&gt; which is local and static.&lt;/li&gt;
&lt;/ul&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;[1] Tips: Don&#39;t mix &lt;/span&gt;&lt;i style=&quot;font-size: small;&quot;&gt;&lt;u&gt;my&lt;/u&gt;&lt;/i&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; with &lt;/span&gt;&lt;i style=&quot;font-size: small;&quot;&gt;&lt;u&gt;local&lt;/u&gt;&lt;/i&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; and don&#39;t touch &lt;/span&gt;&lt;i style=&quot;font-size: small;&quot;&gt;local&lt;/i&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; until you truly believe you know too much about Perl and want to have some fun with it.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;u&gt;Something about subroutines&lt;/u&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;code&gt;sub&lt;/code&gt;: defines a subroutine in Perl, (i.e. function/method). Any passed-in arguments are in an array &lt;code&gt;@_&lt;/code&gt;. Thus, arg1 is stored in &lt;code&gt;@_[0]&lt;/code&gt;, arg2 is stored in &lt;code&gt;@_[1]&lt;/code&gt;, etc. But if this subroutine is in a class (Oh, yes, Perl joins OOP family since v5.12), by default &lt;code&gt;$self&lt;/code&gt; is in &lt;code&gt;@_[0]&lt;/code&gt;, and the rest of arguments are in &lt;code&gt;@_[1...]&lt;/code&gt;.&lt;br /&gt;
Here are two examples. First, a regular subroutine.
&lt;br /&gt;
&lt;blockquote class=&quot;tr_bq&quot;&gt;
&lt;code&gt;sub addTwoNum { my ($n1, $n2) = @_; return $n1 + $n2; }&lt;br /&gt;
my $sum = addTwoNum(1, 2);&lt;/code&gt;&lt;/blockquote&gt;
Next, suppose we have a package/program called Add.pm and is in Local/Add.pm.
&lt;br /&gt;
&lt;blockquote class=&quot;tr_bq&quot;&gt;
&lt;code&gt;sub new { $self = {}; bless $self; return; }&lt;br /&gt;
sub addTwoNum { my ($self, $n1, $n2) = @_; return $n1 + $n2; }&lt;/code&gt;&lt;/blockquote&gt;
To use it,
&lt;br /&gt;
&lt;blockquote class=&quot;tr_bq&quot;&gt;
&lt;code&gt;use Local::Add;&lt;br /&gt;
my $operators = Local::Add-&amp;gt;new;&lt;br /&gt;
my $sum = $operators-&amp;gt;addTwoNum(1, 2);&lt;/code&gt;&lt;/blockquote&gt;
If the argument list is not too long, &lt;code&gt;shift&lt;/code&gt; can be used to replace &lt;code&gt;@_&lt;/code&gt; as shown below.&lt;br /&gt;
&lt;blockquote class=&quot;tr_bq&quot;&gt;
&lt;code&gt;sub addTwoNum { my $n1 = shift; my $n2 = shift; return $n1 + $n2; }&lt;br /&gt;
my $sum = addTwoNum(1, 2);&lt;/code&gt;&lt;/blockquote&gt;
Also, remember, the good thing of copying values to private variables is to turn call-by-reference into call-by-value. See, this code piece
&lt;br /&gt;
&lt;blockquote class=&quot;tr_bq&quot;&gt;
&lt;code&gt;upper($v1, $v2);&lt;br /&gt;
sub upper { for (@_) { tr/a-z/A-Z/ }&lt;/code&gt;&lt;/blockquote&gt;
will change $v1 and $v2; while 
&lt;br /&gt;
&lt;blockquote class=&quot;tr_bq&quot;&gt;
&lt;code&gt;($v3, $v4) = upper($v1, $v2);&lt;br /&gt;
sub upper { my $args = @_; for ($args) { tr/a-z/A-Z/ } return args; }&lt;/code&gt;&lt;/blockquote&gt;
will not change $v1 and $v2.</content><link rel='replies' type='application/atom+xml' href='http://sophie-notes.blogspot.com/feeds/598972915432085970/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/8742529245277406456/598972915432085970' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8742529245277406456/posts/default/598972915432085970'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8742529245277406456/posts/default/598972915432085970'/><link rel='alternate' type='text/html' href='http://sophie-notes.blogspot.com/2012/04/variable-scoping-and-subroutines-in.html' title='Variable scoping and subroutines in Perl'/><author><name>Sophie</name><uri>http://www.blogger.com/profile/08130938292479634191</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgJYm9W1Baj1dfsHW3J9uikPP0eW2P-JuM4uRrJUZI8b-V2_H1C4tGNKzw_wTUUDzxo6VN9r7LKCHoG4ApDuPcc_X2oqejeV_pL9J1K5ezSnXluTppUj60ZBQQBjt73dA/s220/12.jpg'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8742529245277406456.post-699980852671949307</id><published>2012-04-17T18:32:00.001-07:00</published><updated>2012-04-17T18:32:55.264-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Interests"/><category scheme="http://www.blogger.com/atom/ns#" term="SQL"/><title type='text'>SQL Like and Not Like</title><content type='html'>LIKE and NOT LIKE conditions can be used in WHERE clause with wildcards to perform pattern matching.&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;&lt;b&gt;%&lt;/b&gt; allows to match ANY string of ANY length (including zero length)&lt;/li&gt;
&lt;li&gt;&lt;b&gt;_&lt;/b&gt; allows to match on ONE character&lt;/li&gt;
&lt;li&gt;&lt;b&gt;escape character&lt;/b&gt; allows to match a % or a _ character&lt;br /&gt;Note: An escape character can only be defined as a single character (length of 1).&lt;/li&gt;
&lt;/ul&gt;
Suppose we have a table employee with names: jack_smith, jack_howard, jbck_smile, john_smith.&lt;br /&gt;
&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;SELECT * FROM employee WHERE name LIKE &#39;ja%&#39;&lt;/code&gt;&lt;br /&gt;returns with all names starting with &#39;ja&#39;, i.e. jack_smith and jack_howard.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;SELECT * FROM employee WHERE name NOT LIKE &#39;ja%&#39;&lt;/code&gt;&lt;br /&gt;returns with all names not starting with &#39;ja&#39;, i.e. jbck_smile and john_smith.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;SELECT * FROM employee WHERE name LIKE &#39;j_ck%&#39;&lt;/code&gt;&lt;br /&gt;returns with all names starting with &#39;j&#39; and the third and forth characters are &#39;ck&#39;, i.e. jack_smith, jack_howard, and jbck_smile.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;SELECT * FROM employee WHERE name LIKE &#39;%k!_sm%&#39;escape&#39;!&#39;&lt;/code&gt;&lt;br /&gt;returns with all names containing &#39;k_sm&#39;, i.e. jack_smith, jack_howard, and jbck_smile.&lt;/li&gt;
&lt;/ul&gt;</content><link rel='replies' type='application/atom+xml' href='http://sophie-notes.blogspot.com/feeds/699980852671949307/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/8742529245277406456/699980852671949307' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8742529245277406456/posts/default/699980852671949307'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8742529245277406456/posts/default/699980852671949307'/><link rel='alternate' type='text/html' href='http://sophie-notes.blogspot.com/2012/04/sql-like-and-not-like.html' title='SQL Like and Not Like'/><author><name>Sophie</name><uri>http://www.blogger.com/profile/08130938292479634191</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgJYm9W1Baj1dfsHW3J9uikPP0eW2P-JuM4uRrJUZI8b-V2_H1C4tGNKzw_wTUUDzxo6VN9r7LKCHoG4ApDuPcc_X2oqejeV_pL9J1K5ezSnXluTppUj60ZBQQBjt73dA/s220/12.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8742529245277406456.post-8864286551125867513</id><published>2012-03-30T14:01:00.001-07:00</published><updated>2012-03-30T14:01:20.695-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Interests"/><category scheme="http://www.blogger.com/atom/ns#" term="SQL"/><title type='text'>IN vs. EXISTS vs. JOIN on Oracle 10g</title><content type='html'>&lt;div class=&quot;tr_bq&quot;&gt;
There are a whole bunch of discussions about which is winner. Well, the short answer is &quot;It depends&quot;.&lt;/div&gt;
&lt;br /&gt;
This article,&amp;nbsp;&lt;a href=&quot;http://explainextended.com/2009/09/30/in-vs-join-vs-exists-oracle/&quot; target=&quot;_blank&quot;&gt;IN vs. JOIN vs. EXISTS&lt;/a&gt;, posted in Sept, 2009, explained it very well. There was another thread on AskTom but that one was posted in 2000. Oracle 10g has made a lot improvements on IN and EXISTS. Since it is posted in 2009, I assume it is talking about Oracle 10g.&lt;br /&gt;
&lt;br /&gt;
Summary copied from that page:&lt;br /&gt;
&lt;blockquote&gt;
&lt;strong style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;&quot;&gt;IN&lt;/strong&gt;&amp;nbsp;predicate is an ugly duckling of&amp;nbsp;&lt;strong style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;&quot;&gt;SQL&lt;/strong&gt;. Due to the fact it appears inefficient in the eye of an inexperienced developer, it is generally considered non-efficient and&amp;nbsp;&lt;code style=&quot;color: #4488bb; font-weight: bold; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;&quot;&gt;EXISTS&lt;/code&gt;&amp;nbsp;or a&amp;nbsp;&lt;code style=&quot;color: #4488bb; font-weight: bold; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;&quot;&gt;JOIN&lt;/code&gt;&amp;nbsp;are advised to use instead.&lt;br /&gt;
However,&amp;nbsp;&lt;strong style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;&quot;&gt;Oracle&lt;/strong&gt;&amp;nbsp;optimizes&amp;nbsp;&lt;code style=&quot;color: #4488bb; font-weight: bold; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;&quot;&gt;IN&lt;/code&gt;&amp;nbsp;fairly well, using indexes,&amp;nbsp;&lt;code style=&quot;color: #4488bb; font-weight: bold; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;&quot;&gt;UNIQUE&lt;/code&gt;&amp;nbsp;constraints,&amp;nbsp;&lt;code style=&quot;color: #4488bb; font-weight: bold; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;&quot;&gt;NOT NULL&lt;/code&gt;&amp;nbsp;modifiers and other extra information wisely.&lt;br /&gt;
Even in case of duplicates and absence of an index on the subquery field,&amp;nbsp;&lt;strong style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;&quot;&gt;Oracle&lt;/strong&gt;&amp;nbsp;is able to use&amp;nbsp;&lt;code style=&quot;color: #4488bb; font-weight: bold; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;&quot;&gt;HASH SEMI JOIN&lt;/code&gt;&amp;nbsp;method which is more efficient than a&amp;nbsp;&lt;code style=&quot;color: #4488bb; font-weight: bold; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;&quot;&gt;JOIN&lt;/code&gt;&amp;nbsp;/&amp;nbsp;&lt;code style=&quot;color: #4488bb; font-weight: bold; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;&quot;&gt;DISTINCT&lt;/code&gt;&amp;nbsp;solution.&lt;br /&gt;
&lt;code style=&quot;color: #4488bb; font-weight: bold; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;&quot;&gt;EXISTS&lt;/code&gt;&amp;nbsp;is optimized exactly the same, however,&amp;nbsp;&lt;code style=&quot;color: #4488bb; font-weight: bold; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;&quot;&gt;IN&lt;/code&gt;&amp;nbsp;is more readable and concise.&lt;br /&gt;
&lt;strong style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;&quot;&gt;IN&lt;/strong&gt;&amp;nbsp;predicate is exactly what should be use to search records against a list of values in&amp;nbsp;&lt;strong style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;&quot;&gt;Oracle&lt;/strong&gt;.&lt;/blockquote&gt;
&lt;div&gt;
This article, &lt;a href=&quot;http://www.dba-oracle.com/sql/t_subquery_not_in_exists.htm&quot; target=&quot;_blank&quot;&gt;Oracle SQL subquery types in, not in, exists&lt;/a&gt;,&amp;nbsp;also confirmed that IN and EXISTS are pretty much&amp;nbsp;equivalent&amp;nbsp;now. NOTE: It does NOT mean that NOT IN and NOT EXISTS can be&amp;nbsp;exchangeable. That will be a different story.&lt;br /&gt;
&lt;blockquote class=&quot;tr_bq&quot;&gt;
&lt;span class=&quot;style1&quot; style=&quot;font-family: helvetica, verdana; font-size: 8pt;&quot;&gt;&lt;strong&gt;Note:&amp;nbsp;&lt;/strong&gt;&lt;/span&gt;&lt;span style=&quot;font-size: 16px;&quot;&gt;Prior to Oracle10g there was a bug that caused a huge execution difference between EXISTS and IN.&amp;nbsp; Starting in 10g release 2 and beyond, Oracle will automatically rewrite an IN subquery to use the EXISTS clause.&lt;/span&gt;&lt;/blockquote&gt;
&lt;/div&gt;
&lt;div&gt;
Again, it is still hard to tell which would be the winner. Make your decision based on what your table designs. Enjoy~&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://sophie-notes.blogspot.com/feeds/8864286551125867513/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/8742529245277406456/8864286551125867513' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8742529245277406456/posts/default/8864286551125867513'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8742529245277406456/posts/default/8864286551125867513'/><link rel='alternate' type='text/html' href='http://sophie-notes.blogspot.com/2012/03/in-vs-exists-vs-join-on-oracle-10g.html' title='IN vs. EXISTS vs. JOIN on Oracle 10g'/><author><name>Sophie</name><uri>http://www.blogger.com/profile/08130938292479634191</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgJYm9W1Baj1dfsHW3J9uikPP0eW2P-JuM4uRrJUZI8b-V2_H1C4tGNKzw_wTUUDzxo6VN9r7LKCHoG4ApDuPcc_X2oqejeV_pL9J1K5ezSnXluTppUj60ZBQQBjt73dA/s220/12.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8742529245277406456.post-3508425263672326967</id><published>2012-03-29T23:34:00.000-07:00</published><updated>2012-09-09T19:29:37.988-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Interests"/><category scheme="http://www.blogger.com/atom/ns#" term="Linux"/><category scheme="http://www.blogger.com/atom/ns#" term="OS"/><title type='text'>Notes for Linux Commands</title><content type='html'>Keep all my most-used commands here as a reference card. (To-be-Continued~ :)&lt;br /&gt;
&lt;br /&gt;
&lt;code&gt;command --help&lt;/code&gt;&amp;nbsp;Display help and exit&lt;br /&gt;
&lt;code&gt;command --version&lt;/code&gt;&amp;nbsp;Display version information and exit&lt;br /&gt;
&lt;br /&gt;
&lt;code&gt;!!&lt;/code&gt;&amp;nbsp;Execute the last command&lt;br /&gt;
&lt;code&gt;(om[k])&lt;/code&gt;&amp;nbsp;The &lt;i&gt;k&lt;/i&gt;-th latest file. E.g. &lt;code&gt;less service.log.2012*(om[1])&lt;/code&gt; shows the last service log;&lt;br /&gt;
&lt;code&gt;(Om[k])&lt;/code&gt;&amp;nbsp;The&amp;nbsp;&lt;i&gt;k&lt;/i&gt;-th oldest file. E.g.&amp;nbsp;&lt;code&gt;less service.log.2012*(Om[1])&lt;/code&gt;&amp;nbsp;shows the first service log.&lt;br /&gt;
&lt;h2&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;Tools&lt;/span&gt;&lt;/h2&gt;
&lt;code&gt;date&lt;/code&gt; Current date and time.&lt;br /&gt;
&lt;code&gt;cal [month] [year]&lt;/code&gt; Calendar.&lt;br /&gt;
&lt;code&gt;bc&lt;/code&gt; A basic calculator (use &lt;code&gt;quit&lt;/code&gt; to exit).&lt;br /&gt;
&lt;h2&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;System&lt;/span&gt;&lt;/h2&gt;
&lt;code&gt;alias&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;export&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;echo&lt;/code&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;code&gt;&lt;a href=&quot;http://linux.about.com/od/commands/l/blcmdl1_chmod.htm&quot; target=&quot;_blank&quot;&gt;chmod&lt;/a&gt; MODE/&lt;a href=&quot;http://linux.about.com/od/commands/l/blcmdl1_chown.htm&quot; target=&quot;_blank&quot;&gt;chown&lt;/a&gt;/&lt;a href=&quot;http://linux.about.com/od/commands/l/blcmdl1_chgrp.htm&quot; target=&quot;_blank&quot;&gt;chgrp&lt;/a&gt; [OPTION] FILE&lt;/code&gt; Change the mode bits/owner/group ownership of given files and dirctories.&lt;br /&gt;
&lt;br /&gt;
&lt;code&gt;chkconfig&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;modinfo&lt;/code&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;code&gt;groups&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;groupadd&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;groupdel&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;groupmod&lt;/code&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;code&gt;clear&lt;/code&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;code&gt;date&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;cal&lt;/code&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;code&gt;history&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;last&lt;/code&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;code&gt;finger&lt;/code&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;code&gt;uname -a&lt;/code&gt;&lt;br /&gt;
&lt;h2&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;Files&lt;/span&gt;&lt;/h2&gt;
&lt;code&gt;cat&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;more&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;less&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;head&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;tail&lt;/code&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;code&gt;cd&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;ls&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;pwd&lt;/code&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;code&gt;cp&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;mv&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;rm&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;mkdir&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;rmdir&lt;/code&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;code&gt;diff&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;cmp&lt;/code&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;code&gt;grep&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;find&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;&lt;a href=&quot;http://linux.about.com/library/cmd/blcmdl1_sort.htm&quot; target=&quot;_blank&quot;&gt;sort&lt;/a&gt; [option]... [file]...&lt;/code&gt;&amp;nbsp;Sort lines of texts in file(s) and write sorted&amp;nbsp;concatenation&amp;nbsp;of all file(s) to standard output; if file is not given, read from standard input.&lt;br /&gt;
Options:&lt;br /&gt;
&amp;nbsp; &lt;code&gt;-k POS1[,POS2]&lt;/code&gt;&amp;nbsp; &lt;i&gt;POS&lt;/i&gt; is &lt;i&gt;colNum[.charPos][orderingOptions]&lt;/i&gt;, where columns by default are separated by whitespace; &lt;i&gt;charPos&lt;/i&gt; is the character position in the column; &lt;i&gt;orderingOptions&lt;/i&gt; override global ordering options for that key. If no key is given, use the entire line as the key.&lt;br /&gt;
&amp;nbsp; &lt;code&gt;-m&lt;/code&gt;&amp;nbsp; merge already sorted files without further sorting.&lt;br /&gt;
&amp;nbsp; &lt;code&gt;-t SEP&lt;/code&gt;&amp;nbsp; specify separator (instead of whitespace).&lt;br /&gt;
Ordering options:&lt;br /&gt;
&amp;nbsp; &lt;code&gt;-f&lt;/code&gt;&amp;nbsp; fold lower case to upper case characters (ignore case);&lt;br /&gt;
&amp;nbsp; &lt;code&gt;-M&lt;/code&gt;&amp;nbsp; compare (unknown) &amp;lt; `JAN&#39; &amp;lt; ... &amp;lt; `DEC&#39;;&lt;br /&gt;
&amp;nbsp; &lt;code&gt;-n&lt;/code&gt;&amp;nbsp; compare according to string numerical value;&lt;br /&gt;
&amp;nbsp; &lt;code&gt;-r&lt;/code&gt;&amp;nbsp; sort in reverse order.&lt;br /&gt;
Example:&lt;br /&gt;
&amp;nbsp; &lt;code&gt;sort -k7 -r test.txt &amp;gt; test_result.txt&lt;/code&gt; Sort the file by column 7 in reverse order and redirect results to another file.&lt;br /&gt;
&amp;nbsp; &lt;code&gt;ls -al | sort -r -n -k5&lt;/code&gt; Sort the listing results by file size (column 5).&lt;br /&gt;
&lt;br /&gt;
&lt;code&gt;&lt;a href=&quot;http://linux.about.com/library/cmd/blcmdl1_df.htm&quot; target=&quot;_blank&quot;&gt;df&lt;/a&gt; [option] [file]&lt;/code&gt;&amp;nbsp;Disk space usage on a&amp;nbsp;Filesystem. By default, it is shown in 1K block.&lt;br /&gt;
Options:&lt;br /&gt;
&amp;nbsp; &lt;code&gt;-m/g&lt;/code&gt;&amp;nbsp; in 1M/G block;&lt;br /&gt;
&amp;nbsp; &lt;code&gt;-h&lt;/code&gt; &amp;nbsp;human-readable;&lt;br /&gt;
&amp;nbsp; &lt;code&gt;-i&lt;/code&gt;&amp;nbsp;inode information rather than block usage.&lt;br /&gt;
&lt;code&gt;&lt;a href=&quot;http://linux.about.com/library/cmd/blcmdl1_du.htm&quot; target=&quot;_blank&quot;&gt;du&lt;/a&gt; [option] [file]&lt;/code&gt;&amp;nbsp;Summarize Disk space usage of each File, recursively for directories.&lt;br /&gt;
Options:&lt;br /&gt;
&amp;nbsp; &lt;code&gt;-k/m/f&lt;/code&gt;&amp;nbsp; in 1K/M/G block;&lt;br /&gt;
&amp;nbsp; &lt;code&gt;-h&lt;/code&gt;&amp;nbsp; human-readable;&lt;br /&gt;
&amp;nbsp; &lt;code&gt;-c&lt;/code&gt;&amp;nbsp; a grand total for directory (argument) and subdirectories;&lt;br /&gt;
&amp;nbsp; &lt;code&gt;-D&lt;/code&gt;&amp;nbsp; deference symbolic links;&lt;br /&gt;
&amp;nbsp; &lt;code&gt;-s&lt;/code&gt;&amp;nbsp; a total for each argument;&lt;br /&gt;
&amp;nbsp; &lt;code&gt;-S&lt;/code&gt;&amp;nbsp; a total for each argument without subdirectories;&lt;br /&gt;
&amp;nbsp; &lt;code&gt;-X FILE&lt;/code&gt;&amp;nbsp; exclude files that match pattern in FILE.&lt;br /&gt;
&lt;i&gt;df vs. du&lt;/i&gt;: &amp;nbsp;&lt;a href=&quot;http://www.cyberciti.biz/tips/freebsd-why-command-df-and-du-reports-different-output.html&quot; target=&quot;_blank&quot;&gt;Why command df and du reports different output?&lt;/a&gt;&amp;nbsp; &amp;nbsp;&lt;a href=&quot;http://hi.baidu.com/lozn/blog/item/35396d60824e7b40ebf8f8c4.html&quot; target=&quot;_blank&quot;&gt;df和du的区别&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;code&gt;tar&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;gzip&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;bzip2&lt;/code&gt;&lt;br /&gt;
&lt;h2&gt;




&lt;span style=&quot;background-color: white;&quot;&gt;Thread/Process&lt;/span&gt;&lt;/h2&gt;
&lt;ul&gt;
&lt;/ul&gt;
&lt;code&gt;ps&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;kill&lt;/code&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;code&gt;top&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;free&lt;/code&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;code&gt;bg&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;fg&lt;/code&gt;&lt;br /&gt;
&lt;h2&gt;




&lt;span style=&quot;background-color: white;&quot;&gt;Network&lt;/span&gt;&lt;/h2&gt;
&lt;ul&gt;
&lt;/ul&gt;
&lt;code&gt;ifconfig&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;netstat&lt;/code&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;code&gt;ssh&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;sftp&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;rsync&lt;/code&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;code&gt;traceroute&lt;/code&gt;&lt;br /&gt;
&lt;br /&gt;</content><link rel='replies' type='application/atom+xml' href='http://sophie-notes.blogspot.com/feeds/3508425263672326967/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/8742529245277406456/3508425263672326967' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8742529245277406456/posts/default/3508425263672326967'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8742529245277406456/posts/default/3508425263672326967'/><link rel='alternate' type='text/html' href='http://sophie-notes.blogspot.com/2012/03/notes-for-linux-commands.html' title='Notes for Linux Commands'/><author><name>Sophie</name><uri>http://www.blogger.com/profile/08130938292479634191</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgJYm9W1Baj1dfsHW3J9uikPP0eW2P-JuM4uRrJUZI8b-V2_H1C4tGNKzw_wTUUDzxo6VN9r7LKCHoG4ApDuPcc_X2oqejeV_pL9J1K5ezSnXluTppUj60ZBQQBjt73dA/s220/12.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8742529245277406456.post-9104484475307053871</id><published>2012-03-26T00:41:00.001-07:00</published><updated>2012-03-26T00:42:01.243-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Interests"/><category scheme="http://www.blogger.com/atom/ns#" term="Miscellaneous"/><title type='text'>How to deploy Wordpress to AWS EC2</title><content type='html'>Two useful tutorials:&lt;br /&gt;
&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;http://www.ilovefreesoftware.com/23/webware/how-to-setup-free-wordpress-blog-on-amazon-ec2-free-tier.html&quot; target=&quot;_blank&quot;&gt;A easier version using BitNami AMI&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://wp.tutsplus.com/tutorials/scaling-caching/deploy-your-wordpress-blog-to-the-cloud/&quot; target=&quot;_blank&quot;&gt;Do it all by yourself version&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;Ref:&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;
1. AWS - Amazon Web Service. (Yes! THE cloud! And they offer free micro tier for one year now! &amp;nbsp;Bravo! :)&lt;br /&gt;
2. EC2 - Amazon Elastic Compute Cloud.&lt;br /&gt;
3. AMI - Amazon Machion Image. (You can simply think it as which OS you would like to use.)&lt;br /&gt;
4. A bite of intro of BitNami+Wordpress from its &lt;a href=&quot;http://bitnami.org/stack/wordpress&quot; target=&quot;_blank&quot;&gt;official website&lt;/a&gt;.&lt;/span&gt;</content><link rel='replies' type='application/atom+xml' href='http://sophie-notes.blogspot.com/feeds/9104484475307053871/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/8742529245277406456/9104484475307053871' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8742529245277406456/posts/default/9104484475307053871'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8742529245277406456/posts/default/9104484475307053871'/><link rel='alternate' type='text/html' href='http://sophie-notes.blogspot.com/2012/03/how-to-deploy-wordpress-to-aws-ec2.html' title='How to deploy Wordpress to AWS EC2'/><author><name>Sophie</name><uri>http://www.blogger.com/profile/08130938292479634191</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgJYm9W1Baj1dfsHW3J9uikPP0eW2P-JuM4uRrJUZI8b-V2_H1C4tGNKzw_wTUUDzxo6VN9r7LKCHoG4ApDuPcc_X2oqejeV_pL9J1K5ezSnXluTppUj60ZBQQBjt73dA/s220/12.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8742529245277406456.post-2590074902633154627</id><published>2012-03-04T00:10:00.000-08:00</published><updated>2012-03-07T11:04:36.961-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Presentation Skills"/><category scheme="http://www.blogger.com/atom/ns#" term="Programming"/><category scheme="http://www.blogger.com/atom/ns#" term="SQL"/><title type='text'>An interesting and useful post about SQL Joins</title><content type='html'>In&amp;nbsp;&lt;a href=&quot;http://www.codinghorror.com/blog/2007/10/a-visual-explanation-of-sql-joins.html&quot;&gt;A Visual Explanation of SQL Joins&lt;/a&gt;, Jeff Atwood used a simple manner to explain SQL Joins. Also, it is worthy to look into the comments below the blog to get more insights. Enjoy~&lt;br /&gt;
&lt;br /&gt;
Ref: [1]&amp;nbsp;&lt;a href=&quot;http://docs.oracle.com/cd/B19306_01/server.102/b14200/queries006.htm&quot; target=&quot;_blank&quot;&gt;Joins in &lt;oracle® database=&quot;&quot; reference=&quot;&quot; sql=&quot;&quot;&gt;&lt;/oracle®&gt;&lt;/a&gt;&lt;br /&gt;
[2]&amp;nbsp;&lt;a href=&quot;http://www.akadia.com/services/ora_query_tuning.html&quot; target=&quot;_blank&quot;&gt;Oracle SQL Query Tuning Hints&lt;/a&gt;</content><link rel='replies' type='application/atom+xml' href='http://sophie-notes.blogspot.com/feeds/2590074902633154627/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/8742529245277406456/2590074902633154627' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8742529245277406456/posts/default/2590074902633154627'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8742529245277406456/posts/default/2590074902633154627'/><link rel='alternate' type='text/html' href='http://sophie-notes.blogspot.com/2012/03/interesting-and-useful-post-about-sql.html' title='An interesting and useful post about SQL Joins'/><author><name>Sophie</name><uri>http://www.blogger.com/profile/08130938292479634191</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgJYm9W1Baj1dfsHW3J9uikPP0eW2P-JuM4uRrJUZI8b-V2_H1C4tGNKzw_wTUUDzxo6VN9r7LKCHoG4ApDuPcc_X2oqejeV_pL9J1K5ezSnXluTppUj60ZBQQBjt73dA/s220/12.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8742529245277406456.post-486810332084764873</id><published>2012-02-08T18:05:00.001-08:00</published><updated>2012-02-08T18:05:58.355-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Perl"/><category scheme="http://www.blogger.com/atom/ns#" term="Programming"/><title type='text'>Using regular expression with Perl</title><content type='html'>A very good tutorial:&lt;br /&gt;
&lt;a href=&quot;http://affy.blogspot.com/p5be/ch10.htm&quot;&gt;Perl 5 - Regular Expressions&lt;/a&gt;</content><link rel='replies' type='application/atom+xml' href='http://sophie-notes.blogspot.com/feeds/486810332084764873/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/8742529245277406456/486810332084764873' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8742529245277406456/posts/default/486810332084764873'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8742529245277406456/posts/default/486810332084764873'/><link rel='alternate' type='text/html' href='http://sophie-notes.blogspot.com/2012/02/using-regular-expression-with-perl.html' title='Using regular expression with Perl'/><author><name>Sophie</name><uri>http://www.blogger.com/profile/08130938292479634191</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgJYm9W1Baj1dfsHW3J9uikPP0eW2P-JuM4uRrJUZI8b-V2_H1C4tGNKzw_wTUUDzxo6VN9r7LKCHoG4ApDuPcc_X2oqejeV_pL9J1K5ezSnXluTppUj60ZBQQBjt73dA/s220/12.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8742529245277406456.post-5710758716706113397</id><published>2011-12-09T12:56:00.001-08:00</published><updated>2011-12-09T13:00:20.372-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Hardware"/><category scheme="http://www.blogger.com/atom/ns#" term="Interests"/><category scheme="http://www.blogger.com/atom/ns#" term="Kinect"/><title type='text'>How Kinect works</title><content type='html'>&lt;a href=&quot;http://www.wired.com/gadgetlab/2010/11/tonights-release-xbox-kinect-how-does-it-work/all/1&quot;&gt;How does Kinect work?&lt;/a&gt; from Wired.com&lt;br /&gt;
&lt;br /&gt;And another one:&lt;br /&gt;
&lt;a href=&quot;http://gilotopia.blogspot.com/2010/11/how-does-kinect-really-work.html&quot;&gt;How does Kinect really work?&lt;/a&gt;</content><link rel='replies' type='application/atom+xml' href='http://sophie-notes.blogspot.com/feeds/5710758716706113397/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/8742529245277406456/5710758716706113397' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8742529245277406456/posts/default/5710758716706113397'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8742529245277406456/posts/default/5710758716706113397'/><link rel='alternate' type='text/html' href='http://sophie-notes.blogspot.com/2011/12/how-kinect-works.html' title='How Kinect works'/><author><name>Sophie</name><uri>http://www.blogger.com/profile/08130938292479634191</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgJYm9W1Baj1dfsHW3J9uikPP0eW2P-JuM4uRrJUZI8b-V2_H1C4tGNKzw_wTUUDzxo6VN9r7LKCHoG4ApDuPcc_X2oqejeV_pL9J1K5ezSnXluTppUj60ZBQQBjt73dA/s220/12.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8742529245277406456.post-7744727371747811970</id><published>2011-05-12T07:32:00.000-07:00</published><updated>2011-05-13T13:46:25.964-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Interview"/><category scheme="http://www.blogger.com/atom/ns#" term="Programming"/><title type='text'>Copy constructor &amp; Assignment constructor</title><content type='html'>&lt;div&gt;&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot;  style=&quot;font-family:arial;&quot;&gt;Default copy&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;div&gt;Every struct and class have a default copy constructor and assignment operator method. e.g.&lt;/div&gt;&lt;blockquote&gt;&lt;code&gt;1: struct Foo {  int f, o; };&lt;br /&gt;2: Foo f1 = {1, 10};&lt;br /&gt;3: Foo f2(f1);&lt;br /&gt;4: Foo f3; f3 = f1;&lt;/code&gt;&lt;/blockquote&gt;&lt;div&gt;Line 2 invokes the default copy constructor; Line 3 invokes the default assignment operator. The difference of the two is that the copy constructor is invoked when the target (f2) is constructed, passing the source object into the new one; the assignment constructor is invoked when the target object (f3) already exists.&lt;/div&gt;&lt;div&gt;&lt;span class=&quot;Apple-style-span&quot;  style=&quot;font-family:arial;&quot;&gt;&lt;b&gt;When to implement your own copy?&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;The default copy only make a shallow copy. Thus, if one of the members is a pointer, a shallow copy will not allocate memory but create another pointer pointing to the same address. When one of them free the memory, the other one is still pointing to that piece of memory.&lt;/div&gt;&lt;div&gt;In this case, explicit copy and assignment constructor are required.&lt;/div&gt;&lt;div&gt;The difference between the bodies of the two constructors is that the copy constructor can allocate new memory and assign value to the pointer assuming that no member has been set yet (since the object is just being constructed); however, the assignment constructor must handle the case where members may already have been initialized and thus have to delete the contents of the pointer before assigning new value.&lt;/div&gt;&lt;blockquote&gt;&lt;code&gt;class Foobar {&lt;br /&gt;int fo; char* name;&lt;br /&gt;public:&lt;br /&gt;Foobar(const char* str, int n) {&lt;br /&gt;name = new char[strlen(str)+1];&lt;br /&gt;strcpy(name, str);&lt;br /&gt;age = n;&lt;br /&gt;}&lt;br /&gt;Foobar(const Foobar&amp;amp; rf) {&lt;br /&gt;name = new char[strlen(rf.name)+1];&lt;br /&gt;strcpy(name, rf.name);&lt;br /&gt;age = rf.age;&lt;br /&gt;}&lt;br /&gt;Foobar&amp;amp; Foobar(const Foobar&amp;amp; rf) {&lt;br /&gt;&lt;span class=&quot;Apple-style-span&quot;  style=&quot;color:#FF6600;&quot;&gt;     char* temp = new char[strlen(rf.name)+1];&lt;br /&gt;delete[] name;&lt;/span&gt;&lt;br /&gt;name = temp;&lt;br /&gt;strcpy(name, rf.name);&lt;br /&gt;age = rf.age;&lt;br /&gt;&lt;span class=&quot;Apple-style-span&quot;  style=&quot;color:#FF6600;&quot;&gt;     return *this;&lt;/span&gt;&lt;br /&gt;}&lt;br /&gt;~Foobar()  {  delete[] name;  }&lt;br /&gt;};&lt;/code&gt;&lt;/blockquote&gt;&lt;div&gt;Notice that&lt;/div&gt;&lt;div&gt;&lt;ul&gt;&lt;li&gt;why temp is used and why not &lt;code&gt;delete[] name; name  = new char ...&lt;/code&gt;The reason is that the &lt;code&gt;new&lt;/code&gt; may throw an exception and leave the object in a bad state. By finishing all operations that could fail and then replacing the fields, the code is safe.&lt;/li&gt;&lt;li&gt;the assignment operator returns a reference to the object so that &lt;code&gt;f1 = f2 = f3&lt;/code&gt; will work.&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;&lt;div&gt;&lt;span class=&quot;Apple-style-span&quot;  style=&quot;font-family:arial;&quot;&gt;&lt;b&gt;Avoid copy or assignment constructors&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;In some cases, it may not be proper to make a copy of some class fields. To prevent the default implicit copy and assignment constructors, a private copy constructor and assignment operator should be provided. No need to implement the methods. Simply prototyping is enough.&lt;/div&gt;&lt;blockquote&gt;&lt;code&gt;private:&lt;br /&gt;Foobar(const Foobar&amp;amp; rf);&lt;br /&gt;Foobar&amp;amp; Foobar(const Foobar&amp;amp; rf);&lt;/code&gt;&lt;/blockquote&gt;&lt;div&gt;By doing this, if a piece of code attempts to copy the object, there will be an error message saying that the copy constructor and assignment operator cannot be accessed.&lt;/div&gt;&lt;div&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;Ref: &lt;/span&gt;&lt;a href=&quot;http://www.codeguru.com/cpp/cpp/algorithms/general/article.php/c9585&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;Copy Constructors and Assignment Operators&lt;/span&gt;&lt;/a&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://sophie-notes.blogspot.com/feeds/7744727371747811970/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/8742529245277406456/7744727371747811970' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8742529245277406456/posts/default/7744727371747811970'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8742529245277406456/posts/default/7744727371747811970'/><link rel='alternate' type='text/html' href='http://sophie-notes.blogspot.com/2011/05/copy-constructor-assignment-constructor.html' title='Copy constructor &amp; Assignment constructor'/><author><name>Sophie</name><uri>http://www.blogger.com/profile/08130938292479634191</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgJYm9W1Baj1dfsHW3J9uikPP0eW2P-JuM4uRrJUZI8b-V2_H1C4tGNKzw_wTUUDzxo6VN9r7LKCHoG4ApDuPcc_X2oqejeV_pL9J1K5ezSnXluTppUj60ZBQQBjt73dA/s220/12.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8742529245277406456.post-1321796569309934033</id><published>2011-04-23T16:48:00.002-07:00</published><updated>2011-04-23T17:33:00.742-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="All about Apple"/><category scheme="http://www.blogger.com/atom/ns#" term="OS"/><title type='text'>Mac OS X Memory Management: terms in Activity Monitor</title><content type='html'>&lt;div&gt;Last week, I was suffered by the super slow start of VMware Fashion. From the status bar, I can see the usage of CPU is low and the problem seems from memory usage. I opened Activity Monitor to see the detailed memory usage and was surprised to find out that there are only 100MB (or even less!) &quot;free memory&quot;. Btw, I am using Macbook with 2G memory.&lt;/div&gt;&lt;div&gt;This forced me to do more research about the memory management in Mac OS. It turned out that the low amount free memory may not be a bad thing (cf. in Windows, it definitely sucks!).&lt;/div&gt;&lt;div&gt;Let&#39;s start from some general terms here.&lt;/div&gt;&lt;div&gt;You might noticed that, in Activity Monitor, there are: Free, Wired, Active, and Inactive memory. The sum of all these four will be equal to the total amount of RAM (in my case, 2GB).&lt;/div&gt;&lt;div&gt;&lt;ul&gt;&lt;li&gt;Free memory: as named, it is the not-yet-used memory.&lt;/li&gt;&lt;li&gt;Wired memory: here, &quot;wired&quot; = always-in. These data will never be moved to the disk and will always stay in RAM. It could be owned by some core system processes or stand-by processes even after you close the application (main process).&lt;/li&gt;&lt;li&gt;Active memory: These data is currently in RAM and has been recently used.&lt;/li&gt;&lt;li&gt;Inactive memory: here is the tricky part. These data is not actively being used but has recently used. What does it mean? It means that each time when you quit an app, their data will stay in the memory as inactive memory until the app is reopened and access the same data again, OR they are replaced by other apps&#39; data just like free memory. What&#39;s the benefit? Think about &quot;memory frequency&quot;!&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;&lt;div&gt;Mac OS uses a different memory management methodology from what Windows does. It will crawl as more as possible free memory to catch up disk data so that when these data come into use, the access time will be reduced. When the memory usage hits some threshold, it will release some inactive memory; If it is still not enough, it will swap some data from RAM to disk. In Activity Monitor, you can see the cumulative statistics of Page-ins and Page-outs.&lt;/div&gt;&lt;div&gt;Check out &lt;a href=&quot;http://developer.apple.com/library/mac/#documentation/Performance/Conceptual/ManagingMemory/Articles/AboutMemory.html&quot;&gt;this&lt;/a&gt; for more details about memory management and virtual memory.&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://sophie-notes.blogspot.com/feeds/1321796569309934033/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/8742529245277406456/1321796569309934033' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8742529245277406456/posts/default/1321796569309934033'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8742529245277406456/posts/default/1321796569309934033'/><link rel='alternate' type='text/html' href='http://sophie-notes.blogspot.com/2011/04/mac-os-x-memory-management-terms-in.html' title='Mac OS X Memory Management: terms in Activity Monitor'/><author><name>Sophie</name><uri>http://www.blogger.com/profile/08130938292479634191</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgJYm9W1Baj1dfsHW3J9uikPP0eW2P-JuM4uRrJUZI8b-V2_H1C4tGNKzw_wTUUDzxo6VN9r7LKCHoG4ApDuPcc_X2oqejeV_pL9J1K5ezSnXluTppUj60ZBQQBjt73dA/s220/12.jpg'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8742529245277406456.post-1456219831994622817</id><published>2011-03-15T06:52:00.001-07:00</published><updated>2011-03-15T06:58:38.726-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Insterests"/><category scheme="http://www.blogger.com/atom/ns#" term="Programming"/><title type='text'>求二进制数中1的个数</title><content type='html'>比较常见的就是移位计数和快速法了，&lt;a href=&quot;http://www.cnblogs.com/graphics/archive/2010/06/21/1752421.html&quot;&gt;这篇帖子&lt;/a&gt;里列的相当全。</content><link rel='replies' type='application/atom+xml' href='http://sophie-notes.blogspot.com/feeds/1456219831994622817/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/8742529245277406456/1456219831994622817' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8742529245277406456/posts/default/1456219831994622817'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8742529245277406456/posts/default/1456219831994622817'/><link rel='alternate' type='text/html' href='http://sophie-notes.blogspot.com/2011/03/1.html' title='求二进制数中1的个数'/><author><name>Sophie</name><uri>http://www.blogger.com/profile/08130938292479634191</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgJYm9W1Baj1dfsHW3J9uikPP0eW2P-JuM4uRrJUZI8b-V2_H1C4tGNKzw_wTUUDzxo6VN9r7LKCHoG4ApDuPcc_X2oqejeV_pL9J1K5ezSnXluTppUj60ZBQQBjt73dA/s220/12.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8742529245277406456.post-9074424295844774444</id><published>2011-03-11T09:27:00.000-08:00</published><updated>2011-03-11T09:27:56.876-08:00</updated><title type='text'>GNU Screen</title><content type='html'>&lt;div&gt;If you are a UNIX user, you maybe familiar with workspaces, featured in UNIX where you can have different workspaces: one for reading email, one for working, one for Internet surfing, and one for composing documents, blablabla.&lt;/div&gt;&lt;div&gt;Now you can start one terminal and run screen in it. Bang! Problem solved.&lt;/div&gt;&lt;div&gt;Refer to &lt;a href=&quot;http://www.kuro5hin.org/story/2004/3/9/16838/14935&quot;&gt;GNU Screen: an introduction and beginner&#39;s tutorial || kuro5hin.org&lt;/a&gt; for more details.&lt;/div&gt;&lt;div&gt;Here are some commands most used:&lt;/div&gt;&lt;ol&gt;&lt;li&gt;typing &lt;code&gt;screen&lt;/code&gt; will start a screen session for you;&lt;/li&gt;&lt;li&gt;&lt;code&gt;Ctrl-a&lt;/code&gt; and then type &lt;code&gt;c&lt;/code&gt; to create a new session;&lt;/li&gt;&lt;li&gt;&lt;code&gt;Ctrl-a n&lt;/code&gt; to switch to the next or previous session;&lt;/li&gt;&lt;li&gt;&lt;code&gt;Ctrl-a d&lt;/code&gt; to detach from screen sessions and &lt;code&gt;screen -r [session number]&lt;/code&gt; to resume the session you detached. Note that after you detached, all of the screen windows are still running. You can close the terminal, leave your office, go back home, open a terminal on your home machine and resume your work at home!&lt;/li&gt;&lt;/ol&gt;</content><link rel='replies' type='application/atom+xml' href='http://sophie-notes.blogspot.com/feeds/9074424295844774444/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/8742529245277406456/9074424295844774444' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8742529245277406456/posts/default/9074424295844774444'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8742529245277406456/posts/default/9074424295844774444'/><link rel='alternate' type='text/html' href='http://sophie-notes.blogspot.com/2011/03/gnu-screen.html' title='GNU Screen'/><author><name>Sophie</name><uri>http://www.blogger.com/profile/08130938292479634191</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgJYm9W1Baj1dfsHW3J9uikPP0eW2P-JuM4uRrJUZI8b-V2_H1C4tGNKzw_wTUUDzxo6VN9r7LKCHoG4ApDuPcc_X2oqejeV_pL9J1K5ezSnXluTppUj60ZBQQBjt73dA/s220/12.jpg'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8742529245277406456.post-3539781744660000127</id><published>2011-02-27T11:33:00.003-08:00</published><updated>2011-03-01T08:25:40.755-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Programming"/><category scheme="http://www.blogger.com/atom/ns#" term="Python"/><title type='text'>How to run .py file？</title><content type='html'>&lt;div&gt;Suppose Python is installed in &lt;code&gt;/usr/bin/python&lt;/code&gt;.&lt;/div&gt;&lt;ol&gt;&lt;li&gt;In the .py file, add the following line as the &lt;b&gt;first line&lt;/b&gt; to tell shell which interpreter you are using:&lt;br /&gt;&lt;code&gt;#!/usr/bin/python&lt;/code&gt;&lt;/li&gt;&lt;li&gt;Close the file, set it as an executable file&lt;br /&gt;&lt;code&gt;chmod a+x [filename]&lt;/code&gt;&lt;/li&gt;&lt;li&gt;Now you can type &lt;code&gt;./[filename]&lt;/code&gt; to execute it!&lt;/li&gt;&lt;/ol&gt;&lt;div&gt;What if we need some input parameters? We will use &lt;b&gt;command-line arguments&lt;/b&gt; and here is how:&lt;/div&gt;&lt;blockquote&gt;In the .py file, add &lt;code&gt;import sys&lt;/code&gt;, and then grab the arguments from &lt;code&gt;sys.argv[1:]&lt;/code&gt;.&lt;/blockquote&gt;&lt;div&gt;Note 1: &lt;code&gt;sys.argv[0]&lt;/code&gt; is the script name.&lt;br /&gt;Note 2: &lt;code&gt;sys.argv&lt;/code&gt; read in arguments as strings, so if you want integer or float use &lt;code&gt;int()&lt;/code&gt; or &lt;code&gt;float()&lt;/code&gt; to convert them.&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://sophie-notes.blogspot.com/feeds/3539781744660000127/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/8742529245277406456/3539781744660000127' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8742529245277406456/posts/default/3539781744660000127'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8742529245277406456/posts/default/3539781744660000127'/><link rel='alternate' type='text/html' href='http://sophie-notes.blogspot.com/2011/02/how-to-run-py-file.html' title='How to run .py file？'/><author><name>Sophie</name><uri>http://www.blogger.com/profile/08130938292479634191</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgJYm9W1Baj1dfsHW3J9uikPP0eW2P-JuM4uRrJUZI8b-V2_H1C4tGNKzw_wTUUDzxo6VN9r7LKCHoG4ApDuPcc_X2oqejeV_pL9J1K5ezSnXluTppUj60ZBQQBjt73dA/s220/12.jpg'/></author><thr:total>0</thr:total></entry></feed>