<?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%3Arbha033</id>
	<title>SE250:lab-1:rbha033 - 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%3Arbha033"/>
	<link rel="alternate" type="text/html" href="https://wiki.kram.nz/index.php?title=SE250:lab-1:rbha033&amp;action=history"/>
	<updated>2026-04-28T22:21:19Z</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:rbha033&amp;diff=4439&amp;oldid=prev</id>
		<title>Mark: 3 revision(s)</title>
		<link rel="alternate" type="text/html" href="https://wiki.kram.nz/index.php?title=SE250:lab-1:rbha033&amp;diff=4439&amp;oldid=prev"/>
		<updated>2008-11-03T05:18:43Z</updated>

		<summary type="html">&lt;p&gt;3 revision(s)&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;Ok so the task is to report back on &amp;quot;how long it takes to execute an addition (+) operation in C?&amp;quot;&lt;br /&gt;
&lt;br /&gt;
Hints have suggested looping, using the &amp;#039;&amp;#039;&amp;#039;clock()&amp;#039;&amp;#039;&amp;#039; function and using the Linux server.&lt;br /&gt;
&lt;br /&gt;
So Step 1: I&amp;#039;m going to try and remember how to write a C script to loop an addition.&lt;br /&gt;
&lt;br /&gt;
After trying quite hard and referring back to old notes, i kind of remembered how to write a C script again. (YAY!!) so here&amp;#039;s what i did the first time.&lt;br /&gt;
&lt;br /&gt;
  #include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
  #include &amp;lt;time.h&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
  int main(void)&lt;br /&gt;
  {&lt;br /&gt;
 	int answer = 0;&lt;br /&gt;
 	long time;&lt;br /&gt;
 &lt;br /&gt;
  	time=clock();&lt;br /&gt;
 &lt;br /&gt;
 	while (answer&amp;lt;10000000){&lt;br /&gt;
 		answer = answer+1;&lt;br /&gt;
 	}&lt;br /&gt;
   time=clock()-time;&lt;br /&gt;
  &lt;br /&gt;
  printf(&amp;quot;The time taken to reach a million is %ld milliseconds \n&amp;quot;, time);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
Hence used a While loop. I ran it 5 times. &lt;br /&gt;
Here are the results in order in milliseconds: &amp;#039;&amp;#039;&amp;#039;2, 3, 2, 2, 3&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
Not so bad I think. I&amp;#039;m gonna try and use a larger number to reach as 2 or 3 milliseconds is not enough to measure accurately.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This time I&amp;#039;m timing how long it takes for C to reach a billion using simple addition.&lt;br /&gt;
Here are the resultsin milli seconds and in order: &amp;#039;&amp;#039;&amp;#039;2195, 2303, 2217, 2296, 2207&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
So a billion is a good number to count upto.&lt;br /&gt;
&lt;br /&gt;
Now I&amp;#039;m going to try out the &amp;#039;&amp;#039;&amp;#039;long&amp;#039;&amp;#039;&amp;#039; data type.&lt;br /&gt;
Here are the results in milliseconds: &amp;#039;&amp;#039;&amp;#039;2194, 2178, 2167, 2304, 2188&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
Now I&amp;#039;m trying out the &amp;#039;&amp;#039;&amp;#039;short&amp;#039;&amp;#039;&amp;#039; data type.&lt;br /&gt;
Here are the results in milliseconds: &amp;#039;&amp;#039;&amp;#039; NOTHING!!&amp;#039;&amp;#039;&amp;#039; This must be because short data type ends at 32767 and cant go to a billion.&lt;br /&gt;
&lt;br /&gt;
Now trying out the &amp;#039;&amp;#039;&amp;#039;float&amp;#039;&amp;#039;&amp;#039; data type.&lt;br /&gt;
And again nothing works!&lt;br /&gt;
&lt;br /&gt;
There&amp;#039;s something wrong with my code and after consulting John, I&amp;#039;ve been advised to separate the counter so that the changing of the data types doesn&amp;#039;t interfere. Also John has changed my While loop into a For loop. Here&amp;#039;s the code:&lt;br /&gt;
&lt;br /&gt;
 &amp;#039;&amp;#039;#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;time.h&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
 int main(void)&lt;br /&gt;
 {&lt;br /&gt;
 	float answer = 0;&lt;br /&gt;
 	long i;&lt;br /&gt;
 	long time;&lt;br /&gt;
 &lt;br /&gt;
 	time=clock();&lt;br /&gt;
 &lt;br /&gt;
 	for( i = 0; i &amp;lt; 1000000000; i++ ){&lt;br /&gt;
 		answer = answer + 1;&lt;br /&gt;
 	}&lt;br /&gt;
 time=clock()-time;&lt;br /&gt;
 &lt;br /&gt;
 printf(&amp;quot;The time taken to reach a billion is %ld milliseconds \n&amp;quot;, time);&lt;br /&gt;
 }&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
And now the results:&lt;br /&gt;
--- using &amp;#039;&amp;#039;&amp;#039;int&amp;#039;&amp;#039;&amp;#039;: &amp;#039;&amp;#039;&amp;#039;2198, 2345, 2319, 2144, 2347&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
--- using &amp;#039;&amp;#039;&amp;#039;long&amp;#039;&amp;#039;&amp;#039;: &amp;#039;&amp;#039;&amp;#039;2165, 2278, 2144, 2183, 2149&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
--- using &amp;#039;&amp;#039;&amp;#039;short&amp;#039;&amp;#039;&amp;#039;: &amp;#039;&amp;#039;&amp;#039;2424, 2447, 2446, 2547, 2532&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
--- using &amp;#039;&amp;#039;&amp;#039;float&amp;#039;&amp;#039;&amp;#039;: &amp;#039;&amp;#039;&amp;#039;8251, 8193, 8414, 8210, 8491&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
--- using &amp;#039;&amp;#039;&amp;#039;double&amp;#039;&amp;#039;&amp;#039;: &amp;#039;&amp;#039;&amp;#039;8224, 8205, 8253, 8328, 8317&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
Hence, using these results I can conclude that the &amp;#039;&amp;#039;&amp;#039;float&amp;#039;&amp;#039;&amp;#039; and the &amp;#039;&amp;#039;&amp;#039;double&amp;#039;&amp;#039;&amp;#039; types take longer to calculate. It also seems that the &amp;#039;&amp;#039;&amp;#039;long&amp;#039;&amp;#039;&amp;#039; type for some reason is the fastest. So I&amp;#039;m going to test the &amp;#039;&amp;#039;&amp;#039;long&amp;#039;&amp;#039;&amp;#039; type again to see if it spits out similar results.&lt;br /&gt;
Here are the results: &amp;#039;&amp;#039;&amp;#039;2168, 2171, 2187, 2214, 2158&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
So it is true that the &amp;#039;&amp;#039;&amp;#039;long&amp;#039;&amp;#039;&amp;#039; data type did give the fastest time overall using my code. &lt;br /&gt;
&lt;br /&gt;
Question really is, would using the long data type as much as possible make our programs run any faster?&lt;br /&gt;
I think to answer that question correctly, we&amp;#039;d have to consider all the factors that could affect such an experiment.[[User:Rbha033|Rbha033]] 11:49, 4 March 2008 (NZDT)&lt;/div&gt;</summary>
		<author><name>Mark</name></author>
	</entry>
</feed>