<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en-GB">
	<id>https://wiki.kram.nz/index.php?action=history&amp;feed=atom&amp;title=SE250%3Alab-1%3Adcho040</id>
	<title>SE250:lab-1:dcho040 - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.kram.nz/index.php?action=history&amp;feed=atom&amp;title=SE250%3Alab-1%3Adcho040"/>
	<link rel="alternate" type="text/html" href="https://wiki.kram.nz/index.php?title=SE250:lab-1:dcho040&amp;action=history"/>
	<updated>2026-04-29T03:32:38Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.45.3</generator>
	<entry>
		<id>https://wiki.kram.nz/index.php?title=SE250:lab-1:dcho040&amp;diff=4280&amp;oldid=prev</id>
		<title>Mark: 34 revision(s)</title>
		<link rel="alternate" type="text/html" href="https://wiki.kram.nz/index.php?title=SE250:lab-1:dcho040&amp;diff=4280&amp;oldid=prev"/>
		<updated>2008-11-03T05:18:40Z</updated>

		<summary type="html">&lt;p&gt;34 revision(s)&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;==How long does it take to execute an addition (+) operation in C?==&lt;br /&gt;
&lt;br /&gt;
===Information===&lt;br /&gt;
&lt;br /&gt;
*Variable sizes&lt;br /&gt;
 Int   : -2,147,483,648    to 2,147,483,647&lt;br /&gt;
 Long  : -2,147,483,648    to 2,147,483,647&lt;br /&gt;
 Short : -32,768           to 32,767&lt;br /&gt;
 float : (+-)1.175494e-38  to (+-)3.402823e+38&lt;br /&gt;
 double: (+-)2.225074e-308 to (+-)1.797693e+308&lt;br /&gt;
&lt;br /&gt;
*Clock function&lt;br /&gt;
:*In the file &amp;#039;time.h&amp;#039;&lt;br /&gt;
:*Use type &amp;#039;clock_t&amp;#039;&lt;br /&gt;
&lt;br /&gt;
===Expectation===&lt;br /&gt;
* The time of running time would be effected by the range of each type&lt;br /&gt;
&lt;br /&gt;
====Expected order by the speed====&lt;br /&gt;
*1: short        &lt;br /&gt;
*2: int and long&lt;br /&gt;
*3: float         &lt;br /&gt;
*4: double&lt;br /&gt;
&lt;br /&gt;
===Testing codes===&lt;br /&gt;
====Sample1====&lt;br /&gt;
* Codes&lt;br /&gt;
 #define _CRT_SECURE_NO_DEPRECATE&lt;br /&gt;
 #include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;time.h&amp;gt;&lt;br /&gt;
 int main(void)&lt;br /&gt;
 {&lt;br /&gt;
    int a;&lt;br /&gt;
    int b=1;&lt;br /&gt;
    clock_t t0 = clock();&lt;br /&gt;
    for (a=1; a&amp;lt;1000; a++) {&lt;br /&gt;
        b = b+a;&lt;br /&gt;
    }&lt;br /&gt;
    printf(&amp;quot;%d\n&amp;quot;, b);&lt;br /&gt;
    printf(&amp;quot;%d&amp;quot;, clock() - t0);&lt;br /&gt;
    return 0;&lt;br /&gt;
 }&lt;br /&gt;
* Results&lt;br /&gt;
 b = 499501&lt;br /&gt;
 time = 0&lt;br /&gt;
*discussion&lt;br /&gt;
The time is too small -&amp;gt; more calculations are needed&lt;br /&gt;
&lt;br /&gt;
====Sample2====&lt;br /&gt;
* Codes&lt;br /&gt;
 #define _CRT_SECURE_NO_DEPRECATE&lt;br /&gt;
 #include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;time.h&amp;gt;&lt;br /&gt;
 int main(void)&lt;br /&gt;
 {&lt;br /&gt;
    int a;&lt;br /&gt;
    int b=1;&lt;br /&gt;
    clock_t t0 = clock();&lt;br /&gt;
    for (a=1; a&amp;lt;1000000000; a++) {&lt;br /&gt;
        b = b+a;&lt;br /&gt;
    }&lt;br /&gt;
    printf(&amp;quot;%d\n&amp;quot;, b);&lt;br /&gt;
    printf(&amp;quot;%d&amp;quot;, clock() - t0);&lt;br /&gt;
    return 0;&lt;br /&gt;
 }&lt;br /&gt;
* Results&lt;br /&gt;
 b = -1243309311&lt;br /&gt;
 Time = 3475&lt;br /&gt;
*Discussion&lt;br /&gt;
B = -1243309311 because B is bigger than the bigger than 2,147,483,647(maximum int value) -&amp;gt; All calculation should be calculated with short size (the smallest variable: -32,768 to 32,767)&lt;br /&gt;
&lt;br /&gt;
====Sample3====&lt;br /&gt;
* Codes&lt;br /&gt;
 #define _CRT_SECURE_NO_DEPRECATE&lt;br /&gt;
 #include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;time.h&amp;gt;&lt;br /&gt;
 int main(void)&lt;br /&gt;
 {&lt;br /&gt;
    int a, b;&lt;br /&gt;
    int c=0;&lt;br /&gt;
    clock_t t0 = clock();&lt;br /&gt;
    for (a=1; a&amp;lt;32767; a++) {&lt;br /&gt;
        for(b=1; b&amp;lt;32767; b++) { &lt;br /&gt;
           c = c+a-b;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
    printf(&amp;quot;c needs to be 0 = %d\n&amp;quot;, c);&lt;br /&gt;
    printf(&amp;quot;time = %d&amp;quot;, clock() - t0);&lt;br /&gt;
    return 0;&lt;br /&gt;
 }&lt;br /&gt;
* Results&lt;br /&gt;
 c = 0&lt;br /&gt;
 Time = 3892&lt;br /&gt;
*discussion&lt;br /&gt;
While calculate the answer, &amp;#039;c&amp;#039; goes over 32767, yet this looks fine.&lt;br /&gt;
&lt;br /&gt;
====Sample4(final)====&lt;br /&gt;
* Codes&lt;br /&gt;
 #define _CRT_SECURE_NO_DEPRECATE&lt;br /&gt;
 #include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;time.h&amp;gt;&lt;br /&gt;
 int main(void)&lt;br /&gt;
 {&lt;br /&gt;
    int a, b, c=1, d=0;&lt;br /&gt;
    clock_t t0 = clock();&lt;br /&gt;
    for (a=1; a&amp;lt;32767; a++) {&lt;br /&gt;
        for(b=1; b&amp;lt;32767; b++) { &lt;br /&gt;
           d=d+c;&lt;br /&gt;
        }&lt;br /&gt;
        c=c*-1;&lt;br /&gt;
    }&lt;br /&gt;
    printf(&amp;quot;c needs to be 32767 = %d\n&amp;quot;, d);&lt;br /&gt;
    printf(&amp;quot;time = %d&amp;quot;, clock() - t0);&lt;br /&gt;
    return 0;&lt;br /&gt;
 }&lt;br /&gt;
* Results&lt;br /&gt;
 c = 32767&lt;br /&gt;
 Time = 2627&lt;br /&gt;
*Discussion&lt;br /&gt;
:* This example includes ‘*’, ‘+’, and ’-‘ which increase the processing time, yet this lab suggests to use addition (+) operation.&lt;br /&gt;
&lt;br /&gt;
===Testing===&lt;br /&gt;
====Vista(Visual Studio 2008)====&lt;br /&gt;
 Type   Int   long  Short Float Double&lt;br /&gt;
 --------------------------------------&lt;br /&gt;
 Test1  2346  2334  3011  9074  9397&lt;br /&gt;
 Test2  2463  2339  3251  9295  9122&lt;br /&gt;
 Test3  2447  2343  3086  9029  9054&lt;br /&gt;
 Test4  2337  2373  2981  9056  9053&lt;br /&gt;
 Test5  2346  2336  2991  9026  9057&lt;br /&gt;
 --------------------------------------&lt;br /&gt;
 AVG    2388  2345  3064  9096  9137&lt;br /&gt;
*Discussion&lt;br /&gt;
:*The results are not always same.&lt;br /&gt;
::*The speed of a programme is may affected by the condition of the CPU and other running programmes.&lt;br /&gt;
:*‘int’ and ‘long’ are similar, and are the fastest.&lt;br /&gt;
:*’short’ is little slower than ‘int’ and ‘long’&lt;br /&gt;
:*’float’ and ‘double’ are similar, and about 3 times slower than ‘short’&lt;br /&gt;
::*Data types, ‘float’ and ‘double’, have different range, but floating point data may types show same processing speed.&lt;br /&gt;
&lt;br /&gt;
====Vista(CYGWIN)====&lt;br /&gt;
 Type   Int   long  Short Float Double&lt;br /&gt;
 --------------------------------------&lt;br /&gt;
 Test1  3588  3541  6615  5102  5085&lt;br /&gt;
 Test2  3540  3603  6723  5132  5132&lt;br /&gt;
 Test3  3557  3603  6614  5070  5085&lt;br /&gt;
 Test4  3541  3525  6693  5070  5101&lt;br /&gt;
 Test5  3572  3556  6661  5116  5085&lt;br /&gt;
 --------------------------------------&lt;br /&gt;
 AVG    3560  3566  6661  5098  5098&lt;br /&gt;
* Discussion&lt;br /&gt;
:*‘int’ and ‘long’ are similar, and are the fastest.&lt;br /&gt;
:*’float’ and ‘double’ are similar, and are slower than ‘int’ and ‘long’&lt;br /&gt;
::*The difference between ‘float’ and ‘double’, and ‘int’ and ‘long’ are smaller than in Visual Studio 2005.&lt;br /&gt;
:*’Short’ is slower than ‘float and ‘double which is unexpected.&lt;br /&gt;
::*The type, ’short’, may uses difference processing method than ‘int’, and ‘long’&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
*The speed of a programme is may changeable by condition of the CPU and other running programmes.&lt;br /&gt;
*Same range data types may show similar processing speed (e.g. int and long).&lt;br /&gt;
*Floating point data types may show similar processing speed (e.g. float and double).&lt;br /&gt;
*This report suggests some rules with the time, but these rules could be different in other conditions. (e.g. 64bit windows, Linux, different cpus, etc)&lt;/div&gt;</summary>
		<author><name>Mark</name></author>
	</entry>
</feed>