tag:blogger.com,1999:blog-25186808234622043962024-03-05T19:02:18.343-08:00Software Developer at TechTrex.INCAnonymoushttp://www.blogger.com/profile/04738857218698198217noreply@blogger.comBlogger4125tag:blogger.com,1999:blog-2518680823462204396.post-213849950815992842017-03-15T08:28:00.001-07:002017-03-15T08:28:12.439-07:00<div dir="ltr" style="text-align: left;" trbidi="on">
Hello All,<br />
<br />
I have moved my blogs to new website. Also , new blogs will be on following address.<br />
<br />
<a href="http://enigmalink.dynu.com/">Enigmalink</a><br />
<br />
Why I am moving from Blog spot. I wanted to setup my own server and run WordPress. It is a good experience of maintaining own server. So,Please go to link below .</div>
Anonymoushttp://www.blogger.com/profile/04738857218698198217noreply@blogger.com0tag:blogger.com,1999:blog-2518680823462204396.post-78206625189300138172017-02-10T21:07:00.003-08:002017-02-13T20:57:52.004-08:00Sound Sampling Algorithms <div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
<div class="MsoNormal">
<b><span style="font-size: large;">Hello there</span></b>. In this blog, I will show you few approaches to
do sound sampling in C language.</div>
<div class="MsoNormal">
I will not be taking WAV, MP3, or any other file formats,
instead I will generate numbers randomly and use it as a sound input. Reason
because I am not using WAV or any other format because the main purpose of this
blog is to see how we can process big chunk of data in just few seconds with
System efficiency. By system efficiency I mean, using resource of a System like
Memory and CPU as low as possible. So, Let’s Begin with some basics. <o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<h2 style="text-align: left;">
<b><span style="line-height: 107%;"><span style="font-size: x-large;">Basics </span></span></b></h2>
<h2 style="text-align: left;">
<span style="line-height: 107%;"><div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgA5V1w6ZHQTaFkJwt8TVor67jo8aYNmXtRtJaighdB4BAJd2v0KDCchtnZ0s1xyzeo0IwNxXl1_igzzVRJotk0P2yc2FRgOniOX6-g4ZmKCF7S-58T2kS4mnK1pZCSymz0dV_1vtBcufU/s1600/Wav+file+header.PNG" imageanchor="1" style="clear: left; float: left; font-size: xx-large; margin-bottom: 1em; margin-left: 1em;"><img border="0" height="536" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgA5V1w6ZHQTaFkJwt8TVor67jo8aYNmXtRtJaighdB4BAJd2v0KDCchtnZ0s1xyzeo0IwNxXl1_igzzVRJotk0P2yc2FRgOniOX6-g4ZmKCF7S-58T2kS4mnK1pZCSymz0dV_1vtBcufU/s640/Wav+file+header.PNG" width="640" /></a></div>
<div style="text-align: left;">
<span style="font-size: small; font-weight: normal;">Above is the Header information of the Wav file. Until the byte 44, all
the information is related to specification of the Wav file. Actual samples
start after byte 45.</span></div>
<div style="text-align: left;">
<span style="font-weight: normal; text-align: center;"><span style="font-family: inherit; font-size: small;">For more information on the Wav format, here is
the link <a href="http://soundfile.sapp.org/doc/WaveFormat/">Link</a></span></span><br />
<span style="font-weight: normal; text-align: center;"><br /></span>
<span style="font-size: small; text-align: center;">This is the actual WAV file</span></div>
<div style="text-align: left;">
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgGWMpEDeyY_PNKY0GrYJVrcUGhPcttwIdDHxfbZ0UD1CeUqI3MXH-dt3QDtXQtZX9ORAdgcvx86wn9ISRjF1-YycMZFMc6ncXP5PjtrIs3aD-WYCeQBg8Q_nJTFbda_2bgEXq1kjP55a0/s1600/Wav+file.PNG" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" height="356" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgGWMpEDeyY_PNKY0GrYJVrcUGhPcttwIdDHxfbZ0UD1CeUqI3MXH-dt3QDtXQtZX9ORAdgcvx86wn9ISRjF1-YycMZFMc6ncXP5PjtrIs3aD-WYCeQBg8Q_nJTFbda_2bgEXq1kjP55a0/s640/Wav+file.PNG" width="640" /></a></div>
<span style="font-weight: normal; text-align: center;"><br /></span></div>
<br />
<div class="MsoNormal">
<b><span style="line-height: 107%;"><span style="font-size: large;">What Exactly I am going to do?</span><span style="font-size: 14pt;"><o:p></o:p></span></span></b></div>
<div>
<div class="MsoNormal">
<span style="font-size: small;"><span style="font-weight: normal;">I am going through few approaches to build different
algorithms. But question is what exactly I am going to do? I will code three
different approaches adjusting the volume of a sequence of sound samples. Here,
I will have</span> <span style="line-height: 107%;">20,00,00,000 </span><span style="font-weight: normal;">length of </span>16bit<span style="font-weight: normal;"> signed integer array
which will have values between -32768 to 32767 as my sound samples. I will have to calculate those sound samples by the Volume . This is like user is adjusting volume on a device. </span></span><o:p></o:p></div>
<div class="MsoNormal">
<span style="font-size: small;"><span style="font-weight: normal;"><br /></span></span></div>
<div class="MsoNormal">
<b><span style="line-height: 107%;"><span style="font-size: large;">Simple Approach</span><span style="font-size: 14pt;"><o:p></o:p></span></span></b></div>
<div class="MsoNormal">
<b><span style="line-height: 107%;"><span style="font-size: large;"><br /></span></span></b></div>
<div class="MsoNormal">
<span style="font-size: small;"><span style="font-weight: normal;">Code:</span></span></div>
<div class="separator" style="clear: both; text-align: center;">
<span style="font-size: small;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgLWHHiGM23BocvT0Mamh69BHpnSHhaMD4kOiZgNaIT2-jlLS4CznX69hY6FxL1hV0ojKeS99A0umujWKnUaCWMSAXSHkvR2F726qlRbsCQgrWnIvGiDtVsGE0v13_FWGlin1JlJQbExMM/s1600/Lab5simple.c+picture.PNG" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-left: 1em;"><img border="0" height="326" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgLWHHiGM23BocvT0Mamh69BHpnSHhaMD4kOiZgNaIT2-jlLS4CznX69hY6FxL1hV0ojKeS99A0umujWKnUaCWMSAXSHkvR2F726qlRbsCQgrWnIvGiDtVsGE0v13_FWGlin1JlJQbExMM/s640/Lab5simple.c+picture.PNG" width="640" /></a></span></div>
<div class="MsoNormal">
<span style="font-size: small;"><o:p></o:p></span></div>
<br />
<div class="MsoNormal">
<span style="font-size: small;">Compiler Argument</span><span style="font-size: small; font-weight: normal;">: gcc
-O1 -o output program.c<o:p></o:p></span></div>
<div class="separator" style="clear: both; text-align: center;">
</div>
<div class="MsoNormal">
<span style="font-size: small; font-weight: normal;">This code is simple approach and following is the result.</span><o:p></o:p><br />
<span style="font-size: small; font-weight: normal;"><br /></span></div>
</div>
</span><table 1px="" black="" broder:="" solid="">
</table>
<table border="1">
<tbody>
<tr style="border: 1px solid black;">
<th><div style="text-align: left;">
Optimization</div>
</th>
<th><div style="text-align: left;">
Time Elapsed (mSecond)</div>
</th>
<th><div style="text-align: left;">
Memory Usage (MB)</div>
</th>
</tr>
<tr>
<td>0</td>
<td>741</td><td>391.996 </td>
</tr>
<tr>
<td>1</td><td>261</td><td>392.160 </td>
</tr>
<tr>
<td>2</td>
<td>268</td><td>392.160</td>
</tr>
<tr>
</tr>
<tr>
<td>3</td>
<td>229</td><td>392.160</td>
</tr>
<tr>
</tr>
</tbody></table>
<div class="MsoNormal">
<span style="line-height: 107%;"><br /></span>
<br />
<div class="MsoNormal">
<span style="font-size: small; font-weight: normal;">Above table shows when I use optimization level 3 , program’s
elapsed time decreased by 30% and Memory usage fairly stays the same.<o:p></o:p></span><br />
<span style="font-size: small; font-weight: normal;"><br /></span></div>
<div class="MsoNormal">
<span style="font-size: small; font-weight: normal;">Compiler Argument to see Memory and CPU usage :</span><span style="font-size: small; font-weight: normal;"><span style="font-weight: normal;"> “</span><u>command time -v ./output</u><span style="font-weight: normal;"> “.<o:p></o:p></span></span><br />
<span style="font-size: small; font-weight: normal;"><span style="font-weight: normal;"><br /></span></span></div>
<span style="font-size: small; font-weight: normal; line-height: 107%;">
</span><br />
<div class="MsoNormal">
<span style="font-size: small; font-weight: normal;">Below is the screen shout of how output looks like.</span></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgVwVavLpGByvU_gOg4NhXSKK1GVEwR93NVul8WS_mN3IDR8j_l6a-p03bicX2iMumfpgaey4sypemg_-1sEtd00BOf9vZqNytsSHDbIQ27a-AT2VR77982dpGDJRvo7X-pzqDZK2C4jm8/s1600/usage+info.PNG" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" height="408" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgVwVavLpGByvU_gOg4NhXSKK1GVEwR93NVul8WS_mN3IDR8j_l6a-p03bicX2iMumfpgaey4sypemg_-1sEtd00BOf9vZqNytsSHDbIQ27a-AT2VR77982dpGDJRvo7X-pzqDZK2C4jm8/s640/usage+info.PNG" width="640" /></a></div>
<div class="MsoNormal">
<span style="font-size: small; font-weight: normal;"><span style="background-color: white; color: red;"> <o:p></o:p>Note: </span><span style="background-color: yellow;">Package “command” may not be installed by
default on your server. you can get it by apt-get on most of the Ubuntu distribution.</span></span></div>
<div class="MsoNormal">
<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<h2 style="text-align: left;">
<b><span style="font-size: large; line-height: 107%;">Table Look Up</span></b></h2>
</div>
<div class="MsoNormal">
<span style="font-size: small; font-weight: normal;">In table look up approach, I pre-calculated the samples from
0 to 32768 by Volume. <o:p></o:p></span><br />
<span style="font-size: small; font-weight: normal;"><br /></span></div>
<div class="MsoNormal">
<span style="font-size: small; font-weight: normal;">This is the code:</span><o:p></o:p><br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgYv55Pod1LjkTIQQrpbuqXeDAi4pLhQY6mVSGKl8vZxFjDyS-JhNJO4cP3UhhcaxcnkjnOuHOJCI2MjIHepyg-5sk_i_VDBwTxohpjYBC9prhIg_2bjITNk4MZa-k3BmPLgKrpk6y2Y5U/s1600/Tablelookup.PNG" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" height="350" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgYv55Pod1LjkTIQQrpbuqXeDAi4pLhQY6mVSGKl8vZxFjDyS-JhNJO4cP3UhhcaxcnkjnOuHOJCI2MjIHepyg-5sk_i_VDBwTxohpjYBC9prhIg_2bjITNk4MZa-k3BmPLgKrpk6y2Y5U/s640/Tablelookup.PNG" width="640" /></a></div>
<span style="font-size: small; font-weight: normal;"><br /></span>
</div>
<div class="MsoNormal">
<span style="font-size: small; font-weight: normal;">For this approach the elapsed time and memory usage were significantly
changed. This algorithm take more time and memory than simple approach.</span><o:p></o:p><br />
<span style="font-size: small; font-weight: normal;"><br /></span></div>
</div>
<table border="1">
<tbody style="border: 1px solid black;">
<tr>
<th><div style="text-align: left;">
Optimization</div>
</th>
<th><div style="text-align: left;">
Time Elapsed (Second)</div>
</th>
<th><div style="text-align: left;">
Memory Usage (MB)</div>
</th>
</tr>
<tr>
<td>0</td>
<td>2.121</td><td>782.284</td>
</tr>
<tr>
<td>1</td><td>1.180</td><td>782.760 </td>
</tr>
<tr>
<td>2</td>
<td>1.117</td><td>782.760</td>
</tr>
<tr>
</tr>
<tr>
<td>3</td>
<td>1.117</td><td>782.856</td>
</tr>
<tr>
</tr>
</tbody></table>
<div class="MsoNormal">
<b><span style="font-size: 14.0pt; line-height: 107%;"><br /></span></b>
<br />
<h2 style="text-align: left;">
<b><span style="font-size: large; line-height: 107%;">Memoization</span></b></h2>
</div>
<div class="MsoNormal">
<span style="font-size: small; font-weight: normal;">In this approach, main purpose is to eliminate repeating
calculations. Once we calculate a sample by Volume factor, we store it in the
array and next time use it instead of Multiplying. <o:p></o:p></span><br />
<span style="font-size: small; font-weight: normal;"><br /></span></div>
<div class="MsoNormal">
<span style="font-size: small; font-weight: normal;">Below is how code looks like. </span><o:p></o:p><br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjPjm-DtGq0tI1MAVarHfzChyphenhyphenu1MfXzLuZyOjq5zgXmhWqjSts2H-Nb36gx6Zo7iF8Tij5b_wZFyt3TRw2-BJ3O0fE_-ORA_zXCSjNGZomqIsHEkTfvrIdS40HYcB0TJSFJAc1aLNCIWMc/s1600/Memoization.PNG" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" height="540" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjPjm-DtGq0tI1MAVarHfzChyphenhyphenu1MfXzLuZyOjq5zgXmhWqjSts2H-Nb36gx6Zo7iF8Tij5b_wZFyt3TRw2-BJ3O0fE_-ORA_zXCSjNGZomqIsHEkTfvrIdS40HYcB0TJSFJAc1aLNCIWMc/s640/Memoization.PNG" width="640" /></a></div>
<span style="font-size: small; font-weight: normal;"><br /></span>
<span style="font-size: small; font-weight: normal;"><br /></span></div>
<table border="1">
<tbody>
<tr>
<th><div style="text-align: left;">
Optimization</div>
</th>
<th><div style="text-align: left;">
Time Elapsed (Second)</div>
</th>
<th><div style="text-align: left;">
Memory Usage (MB)</div>
</th>
</tr>
<tr>
<td>0</td>
<td>2.847</td><td>782.692</td>
</tr>
<tr>
<td>1</td><td>0.679</td><td>782.764 </td>
</tr>
<tr>
<td>2</td>
<td>0.547</td><td>782.736</td>
</tr>
<tr>
</tr>
<tr>
<td>3</td>
<td>0.545</td><td>782.760</td>
</tr>
<tr>
</tr>
</tbody></table>
<br />
<div class="MsoNormal">
<span style="font-size: small; font-weight: normal;">This approach cost us less time compare to the table lookup.
I</span><span style="font-size: small; font-weight: normal;">n average, out of 20,00,00,000 total samples only 41,905</span><span style="font-size: small; font-weight: normal;"> time calculation happen
and all the other values just got copied from an array . So, this is the fairly
improvement in terms of CPU stress. </span><o:p></o:p><br />
<br /></div>
</h2>
</div>
</div>
Anonymoushttp://www.blogger.com/profile/04738857218698198217noreply@blogger.com0tag:blogger.com,1999:blog-2518680823462204396.post-88313958284107274812017-02-03T19:51:00.003-08:002017-02-03T19:51:32.339-08:00SPO600 Lab04 Compiler Options<div dir="ltr" style="text-align: left;" trbidi="on">
In this Lab I have learn about "gcc" compiler options to optimize C code. <br />
<br />
To begging lets use following 3 compile options.<br />
<b><span style="font-size: large;">-g</span></b> is to enable debugging information.<a href="https://gcc.gnu.org/onlinedocs/gcc/Debugging-Options.html">More Information</a>.<br />
<br />
<b><span style="font-size: large;">-O0</span></b> to tell compiler which level of optimization to apply on Code. (Capital letter and then digit Zero).<a href="http://www.rapidtables.com/code/linux/gcc/gcc-o.htm">More Information</a><br />
<br />
<span style="font-size: large;"><b>-fno-builtin</b></span> is to tell compiler to not use built in function optimization. We will look in a bit.<br />
<div>
<br />
We will be adding and removing the compile option to compare some factors like code size, sections in assembly .<br />
<br />
<h2 style="text-align: left;">
<b><span style="font-size: large;">1. Lets compile the following basic Hello World code.</span></b></h2>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiiYvjF9oULDM52xzq4GOIGxbZ1GTA2yFPdjneeWHdqivhLxGBzlbSeca7Ct0sxj1Ndh1jjYx9TybG6FWCIa7ohE8mFjzO3ydy45HKE-2DHLU1fK0yvOKZ8O_yvNJIxtH17rW0B5S6bBgs/s1600/Helloworld.PNG" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" height="80" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiiYvjF9oULDM52xzq4GOIGxbZ1GTA2yFPdjneeWHdqivhLxGBzlbSeca7Ct0sxj1Ndh1jjYx9TybG6FWCIa7ohE8mFjzO3ydy45HKE-2DHLU1fK0yvOKZ8O_yvNJIxtH17rW0B5S6bBgs/s400/Helloworld.PNG" width="400" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div style="text-align: left;">
<span style="background-color: yellow;">Compile Argument</span><span style="font-size: large;"> </span>: <b>gcc -g -O0 -fno-builtin hello.c </b></div>
<div style="text-align: left;">
<br /></div>
<div style="text-align: left;">
From above compile , the executable file size was 11K. </div>
<div style="text-align: left;">
To see the compiled object assembly code, I used <b>objdump -d | less.</b> This command will dump executable file in less so, we can see the output . </div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhW_4iWOFNtb4RYTaW8EVYmfMf9HqbfZv4AIE1cokx-mhyn9ft5mzMk2kU5zQILyOJn68MJ5xuui8vjsgAvadE_aqKC4nv0el1vwykjR2zRqe4acYkgLHBQMjnD1wg0r52uy9FpJqo6b_Q/s1600/1+with+-g+-O0+and+fno-builtin.PNG" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" height="180" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhW_4iWOFNtb4RYTaW8EVYmfMf9HqbfZv4AIE1cokx-mhyn9ft5mzMk2kU5zQILyOJn68MJ5xuui8vjsgAvadE_aqKC4nv0el1vwykjR2zRqe4acYkgLHBQMjnD1wg0r52uy9FpJqo6b_Q/s640/1+with+-g+-O0+and+fno-builtin.PNG" width="640" /></a></div>
<div style="text-align: left;">
<br /></div>
<div style="text-align: left;">
<b><br /></b></div>
<div style="text-align: left;">
<b><br /></b></div>
<div style="text-align: left;">
<b><br /></b></div>
<div style="text-align: left;">
<b><br /></b></div>
<div style="text-align: left;">
<b><br /></b></div>
<div style="text-align: left;">
<b><br /></b></div>
<div style="text-align: left;">
Above snippet is how main function looks like in assembly . </div>
<div style="text-align: left;">
<br /></div>
<div style="text-align: left;">
<h2 style="text-align: left;">
<b><span style="font-size: large;">2. Second step is to add the -static option in compile option. </span></b></h2>
</div>
<div style="text-align: left;">
</div>
<div style="text-align: left;">
<h3 style="text-align: left;">
<span style="font-size: small;"><span style="background-color: yellow;">Compile Argument</span> </span>: <b>gcc -g -O0 -fno-builtin -static hello.c</b></h3>
</div>
<br />
<div>
<div>
<b>Q:What happens when we use static option while compile?</b></div>
<div>
<div>
<b><span style="font-size: large;">A: </span></b>It tells the compiler to load assembly for the whole library used by the program.</div>
<div>
So when program runs, it doesn't have to go through link table to find the function in library.</div>
<div>
This can improve performance but executable size will increase a lot. </div>
<div>
</div>
<div>
On this compile file size jumped to 895K.</div>
<div>
From the snippet below , there are two new lines added at the end of the main section. </div>
<div>
These are padding so that program instructions can end at a particular memory boundary.</div>
</div>
</div>
<div>
<br /></div>
<div style="text-align: left;">
</div>
<div class="separator" style="clear: both; text-align: center;">
</div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgTNdSKa5WFH55OgmnlAf1NQgWNjD4Qly-IozNHbxEpCSrckkxnHdQKGTdoMJWQVRCPb2LQIWddtNyxGHLs0-WX9MhTiGyGTq7Si-Ojmr7u43_pcZPqS8Eepp8HDUBrzcpfc_JfVKLGBrg/s1600/2_static.PNG" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-left: 1em;"><img border="0" height="188" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgTNdSKa5WFH55OgmnlAf1NQgWNjD4Qly-IozNHbxEpCSrckkxnHdQKGTdoMJWQVRCPb2LQIWddtNyxGHLs0-WX9MhTiGyGTq7Si-Ojmr7u43_pcZPqS8Eepp8HDUBrzcpfc_JfVKLGBrg/s640/2_static.PNG" width="640" /></a></div>
<div style="text-align: left;">
<br /></div>
<div style="text-align: left;">
<br /></div>
<div style="text-align: left;">
<br /></div>
<div style="text-align: left;">
<br /></div>
<div style="text-align: left;">
<br /></div>
<div style="text-align: left;">
<br /></div>
<div style="text-align: left;">
<br /></div>
<div style="text-align: left;">
<br /></div>
<div style="text-align: left;">
<br /></div>
<div style="text-align: left;">
<br /></div>
<div style="text-align: left;">
<br /></div>
<div style="text-align: left;">
<br /></div>
<div style="text-align: left;">
<h2 style="text-align: left;">
<span style="font-size: large;"><b>3. Third step , compile without the -fno-builtin</b></span></h2>
</div>
<div>
<span style="background-color: yellow;"><b>Compile Argument</b> </span>: gcc -g -O0 -static hello.c</div>
<div>
<br /></div>
<div>
-fno-builtin asks the compiler not to do any optimization for built in functions, in our case <u>printf</u>.</div>
<div>
If we remove -fno-builtin from compile, then compiler will optimize the printf .</div>
<div>
After the objdump , if we look at the function call for printing in the section main, it is changed to <_IO_puts></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjNlsABdGAi0wLlxwDnPmquig8GCgQ55XxljAt7Ki4v6g1G8IGOi4ECs7rPZXBhAEPtnSc8vzfOxkdxh5kmaSnpf32K1Jag_yc0ZWrbpitAne35F3P7sUmjrewN3IuIsVscIWDkSc_et6A/s1600/3_removed_-fnobuilin.PNG" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-left: 1em;"><img border="0" height="126" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjNlsABdGAi0wLlxwDnPmquig8GCgQ55XxljAt7Ki4v6g1G8IGOi4ECs7rPZXBhAEPtnSc8vzfOxkdxh5kmaSnpf32K1Jag_yc0ZWrbpitAne35F3P7sUmjrewN3IuIsVscIWDkSc_et6A/s640/3_removed_-fnobuilin.PNG" width="640" /></a></div>
<div>
<br /></div>
<div>
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
</div>
<div style="text-align: left;">
<br /></div>
<br />
<div>
<h2 style="text-align: left;">
<b>4. Removing the compiler option -g .</b></h2>
</div>
<div>
<div>
<b style="background-color: yellow;">Compile Argument </b>: gcc -O0 -fno-builtin hello.c </div>
<div>
<br /></div>
<div>
Now lets compare the object with the <b>gcc -g -O0 -fno-builtin hello.c</b> ( First step ).</div>
<div>
<br /></div>
<div>
1. Code size reduced by 20% (9K)</div>
<div>
2. From objdump ,sections like .debug_line, .debug_str, .debug_abbrev , .debug_aranges,.debug_info are removed.</div>
</div>
<div>
<br /></div>
<div>
<span style="background-color: #999999;">Debugging Section Headers.</span></div>
<div>
<br /></div>
<div>
1. debug_info : describes the program's debug information for ensuring simple data types, variables and function are being used correctly.</div>
<div>
<br /></div>
<div>
2. debug_types : ensuring complex data types, like arrays, list, struct are being uses correctly </div>
<div>
<br /></div>
<div>
3. debug_str section: contains strings that are used more than once. </div>
<div>
<br /></div>
<div>
<span style="font-size: large;"><b>5.</b> <b>Some Optimization level</b></span><br />
<table class="dtable" style="width: 600;">
<tbody>
<tr>
<th>Option</th>
<th>Optimization Level</th>
<th>execution time</th>
<th>code size</th>
<th>memory usage</th>
<th>compile time</th>
</tr>
<tr>
<td>-O0</td>
<td>optimization for compilation time (default)</td>
<td align="center">+</td>
<td align="center">+</td>
<td align="center">-</td>
<td align="center">-</td>
</tr>
<tr>
<td>-O1 or -O</td>
<td>optimization for code size and execution time</td>
<td align="center">-</td>
<td align="center">-</td>
<td align="center">+</td>
<td align="center">+</td>
</tr>
<tr>
<td>-O2</td>
<td>optimization more for code size and execution time</td>
<td align="center">--</td>
<td align="center"></td>
<td align="center">+</td>
<td align="center">++</td>
</tr>
<tr>
<td>-O3</td>
<td>optimization more for code size and execution time</td>
<td align="center">---</td>
<td align="center"></td>
<td align="center">+</td>
<td align="center">+++</td>
</tr>
<tr>
<td>-Os</td>
<td>optimization for code size</td>
<td align="center"></td>
<td align="center">--</td>
<td align="center"></td>
<td align="center">++</td>
</tr>
<tr>
<td>-Ofast</td>
<td>O3 with fast none accurate math calculations</td>
<td align="center">---</td>
<td align="center"></td>
<td align="center">+</td>
<td align="center">+++</td>
</tr>
</tbody></table>
<span style="background-color: #fffff8; color: #222222; font-family: Arial, Verdana, Helvetica, sans-serif; font-size: 14px;"><br /></span></div>
<div>
<span style="background-color: #fffff8; color: #222222; font-family: Arial, Verdana, Helvetica, sans-serif; font-size: 14px;">+increase, ++increase more, +++increase even more. -reduce ,--reduce more, ---reduce even more</span></div>
<div>
<br /></div>
<div>
<b><span style="font-size: large;">7. Calling printf within a method. </span></b></div>
<div>
<br /></div>
<div>
This section , we will create the function output() with printf inside. And, call output function from main(). Code looks like following snippet.</div>
<div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj5NQe96iatgXdx0Drq6kW0hQu_beSfzRovQHawn9VKsjixXT7w-yi1mtkMRHAGR32RXXgTSsA3iybSR7D3wJUg9qBpyyygz8qwdmEzUo4H4petBXTsYV05j2PYwdGLKB3ZfuO1U-CK2f0/s1600/output+fucntion.PNG" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-left: 1em;"><img border="0" height="200" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj5NQe96iatgXdx0Drq6kW0hQu_beSfzRovQHawn9VKsjixXT7w-yi1mtkMRHAGR32RXXgTSsA3iybSR7D3wJUg9qBpyyygz8qwdmEzUo4H4petBXTsYV05j2PYwdGLKB3ZfuO1U-CK2f0/s400/output+fucntion.PNG" width="400" /></a></div>
<br />
<br />
<br /></div>
<div>
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<b style="background-color: yellow;">Compile Argument </b>: <b>gcc -O0 -fno-builtin hello.c</b><br />
<br />
Now,objdump that executable and one can see new section is being created for the output function.<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgpTIyFxyrUDX63AcPPy48NC5UqAm71z7VHVZPJ5fNAgByQD7O-uaGx00SGa0PBMMWrovPp2uyCtXlC4Gbr3bXdyyHy1irMotICfXdeBpVQecctFdzzSADdM6f4Rk19usmxBS_ax9h3a5Y/s1600/5_printf_outputfucntion.PNG" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-left: 1em;"><img border="0" height="358" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgpTIyFxyrUDX63AcPPy48NC5UqAm71z7VHVZPJ5fNAgByQD7O-uaGx00SGa0PBMMWrovPp2uyCtXlC4Gbr3bXdyyHy1irMotICfXdeBpVQecctFdzzSADdM6f4Rk19usmxBS_ax9h3a5Y/s640/5_printf_outputfucntion.PNG" width="640" /></a></div>
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
So, new section will be created for each method.<br />
<br />
<br />
We have seen , optimization , assembly section and function call. There are lot more options for gcc compiler if you look at the man page. Some option may improve performance of program but the executable size would be bigger and for other options is completely opposite. These days, programmers dont have to think about writing code in optimized way because compiler can do it for them . However, Algorithm , approach and some other factors which still depends on programmer.<br />
<br />
<br />
</div>
</div>
</div>
Anonymoushttp://www.blogger.com/profile/04738857218698198217noreply@blogger.com0tag:blogger.com,1999:blog-2518680823462204396.post-47871921535797893152017-01-12T15:03:00.001-08:002017-01-12T15:03:19.564-08:00<div dir="ltr" style="text-align: left;" trbidi="on">
Hello All,<br />
<br />
This is Shiv Gajiwala.<br />
<br />
Test Blog.<br />
<br /></div>
Anonymoushttp://www.blogger.com/profile/04738857218698198217noreply@blogger.com0