<?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%3Arwan064</id>
	<title>SE250:lab-1:rwan064 - 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%3Arwan064"/>
	<link rel="alternate" type="text/html" href="https://wiki.kram.nz/index.php?title=SE250:lab-1:rwan064&amp;action=history"/>
	<updated>2026-04-18T07:51:49Z</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:rwan064&amp;diff=4478&amp;oldid=prev</id>
		<title>Mark: 25 revision(s)</title>
		<link rel="alternate" type="text/html" href="https://wiki.kram.nz/index.php?title=SE250:lab-1:rwan064&amp;diff=4478&amp;oldid=prev"/>
		<updated>2008-11-03T05:18:44Z</updated>

		<summary type="html">&lt;p&gt;25 revision(s)&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;==Introduction==&lt;br /&gt;
&lt;br /&gt;
Timing the addition operation (+) in C.&lt;br /&gt;
&lt;br /&gt;
Need to time the addition operation for the following data types:&lt;br /&gt;
* int&lt;br /&gt;
* long&lt;br /&gt;
* short&lt;br /&gt;
* float&lt;br /&gt;
* double&lt;br /&gt;
&lt;br /&gt;
==Methods==&lt;br /&gt;
&lt;br /&gt;
The first method that came to mind was to make a function that does addition operations a large number of times (but less than the limit for that data range) and then time how long it takes to execute that function.&lt;br /&gt;
&lt;br /&gt;
Assumptions:&lt;br /&gt;
* Time taken to run and exit from a function is negligible&lt;br /&gt;
* Time taken to declare and compare variables is also negligible&lt;br /&gt;
&lt;br /&gt;
===Timing the functions===&lt;br /&gt;
The macro used to time the functions. The macro assumes variables named &amp;quot;t1&amp;quot;, &amp;quot;t2&amp;quot; and &amp;quot;time&amp;quot; are already declared in the main function of types &amp;quot;clock_t&amp;quot;, &amp;quot;clock_t&amp;quot; and &amp;quot;double&amp;quot; respectively.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#define TEST(fname, limit) \&lt;br /&gt;
	t1 = clock(); \&lt;br /&gt;
	fname(); \&lt;br /&gt;
	t2 = clock(); \&lt;br /&gt;
	printf( #fname &amp;quot;, Time elapsed: %f\n&amp;quot;, (double) (t1 - t2) / (double) CLOCKS_PER_SEC ); \&lt;br /&gt;
	time = ((double) (t1 - t2) / (double) CLOCKS_PER_SEC) / limit; \&lt;br /&gt;
	printf( &amp;quot;Time for + operation: %f\n&amp;quot;, time );&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The limits for the loops used to test each data type.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#define INT_LIMIT 2147483647&lt;br /&gt;
#define LONG_LIMIT 2147483647&lt;br /&gt;
#define SHORT_LIMIT 32767&lt;br /&gt;
&lt;br /&gt;
// For float and double the pre-defined macros in &amp;quot;float.h&amp;quot; were used.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The actual functions that are timed and does the addition operations. The addition operation in the for loop function is done in the incrementing section of the loop and in the while loop function it is done inside the loop.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
void test1( void ); // INT&lt;br /&gt;
void test2( void ); // INT&lt;br /&gt;
void test3( void ); // LONG&lt;br /&gt;
void test4( void ); // LONG&lt;br /&gt;
void test5( void ); // SHORT&lt;br /&gt;
void test6( void ); // SHORT&lt;br /&gt;
void test7( void ); // FLOAT&lt;br /&gt;
void test8( void ); // FLOAT&lt;br /&gt;
void test9( void ); // DOUBLE&lt;br /&gt;
void test10( void ); // DOUBLE&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
void test1( void )&lt;br /&gt;
{&lt;br /&gt;
	int i;&lt;br /&gt;
	for ( i = 0; i &amp;lt; INT_LIMIT; i++ ) {&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void test2( void )&lt;br /&gt;
{&lt;br /&gt;
	int i = 0;&lt;br /&gt;
	while ( i &amp;lt; INT_LIMIT ) {&lt;br /&gt;
		i++;&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// ... similar for other data types. Each type has two tests.&lt;br /&gt;
// One with a for loop and another with a while loop.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The main function showing how the tests are run.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int main( int argc, char *argv[] )&lt;br /&gt;
{&lt;br /&gt;
	clock_t t1, t2;&lt;br /&gt;
	&lt;br /&gt;
	double time = 0.0;&lt;br /&gt;
	&lt;br /&gt;
	TEST( test1, INT_LIMIT );&lt;br /&gt;
	TEST( test2, INT_LIMIT );&lt;br /&gt;
	puts( &amp;quot;\n&amp;quot; );&lt;br /&gt;
	TEST( test3, LONG_LIMIT );&lt;br /&gt;
	TEST( test4, LONG_LIMIT );&lt;br /&gt;
	puts( &amp;quot;\n&amp;quot; );&lt;br /&gt;
	TEST( test5, SHORT_LIMIT );&lt;br /&gt;
	TEST( test6, SHORT_LIMIT );&lt;br /&gt;
	puts( &amp;quot;\n&amp;quot; );&lt;br /&gt;
	TEST( test7, FLT_MAX );&lt;br /&gt;
	TEST( test8, FLT_MAX );&lt;br /&gt;
	puts( &amp;quot;\n&amp;quot; );&lt;br /&gt;
	TEST( test9, DOUBLE_LIMIT );&lt;br /&gt;
	TEST( test10, DOUBLE_LIMIT );&lt;br /&gt;
	&lt;br /&gt;
	return 0;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Results==&lt;br /&gt;
Results from different compilers and environments&lt;br /&gt;
===Windows Vista: cygwin===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
test1, Time elapsed: 4294963.375000&lt;br /&gt;
test2, Time elapsed: 4294963.421000&lt;br /&gt;
test3, Time elapsed: 4294963.436000&lt;br /&gt;
test4, Time elapsed: 4294963.437000&lt;br /&gt;
test5, Time elapsed: 0.000000&lt;br /&gt;
test6, Time elapsed: 0.000000&lt;br /&gt;
test7, Time elapsed: 0.000000&lt;br /&gt;
test8, Time elapsed: 0.000000&lt;br /&gt;
test9, Time elapsed: 0.000000&lt;br /&gt;
test10, Time elapsed: 0.000000&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Linux server===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
test1, Time elapsed: -34.890000&lt;br /&gt;
test2, Time elapsed: -64.460000&lt;br /&gt;
test3, Time elapsed: -34.930000&lt;br /&gt;
test4, Time elapsed: -64.070000&lt;br /&gt;
test5, Time elapsed: 0.000000&lt;br /&gt;
test6, Time elapsed: 0.000000&lt;br /&gt;
test7, Time elapsed: 0.000000&lt;br /&gt;
test8, Time elapsed: 0.000000&lt;br /&gt;
test9, Time elapsed: -0.010000&lt;br /&gt;
test10, Time elapsed: 0.000000&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Windows XP: Visual Studio 2005===&lt;br /&gt;
Compiled using the command line compiler.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
test1, Time elapsed: -6.390000&lt;br /&gt;
Time for + operation: -0.000000&lt;br /&gt;
test2, Time elapsed: -6.297000&lt;br /&gt;
Time for + operation: -0.000000&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
test3, Time elapsed: -6.313000&lt;br /&gt;
Time for + operation: -0.000000&lt;br /&gt;
test4, Time elapsed: -6.296000&lt;br /&gt;
Time for + operation: -0.000000&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
test5, Time elapsed: 0.000000&lt;br /&gt;
Time for + operation: 0.000000&lt;br /&gt;
test6, Time elapsed: 0.000000&lt;br /&gt;
Time for + operation: 0.000000&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
After test6 the program kept running for about 5 minutes without any output so I terminated the process. I think it takes too long because I am either doing too many calculations or there is a condition that never evaluates to false.&lt;br /&gt;
&lt;br /&gt;
==Conclusions==&lt;br /&gt;
When using cygwin on Windows Vista (in GCL) I got proper (positive) values for the time taken to do the addition operation. But when I tried it using the Linux server from the same machine I got negetive values. Finally I tried a third time on the Engineering Lab using Windows XP and the command line Visual Studio 2005 compiler and also got negetive values.&lt;br /&gt;
&lt;br /&gt;
I have no idea why I get negetive values since the &amp;quot;clock&amp;quot; function returns the number of clock ticks elapsed since the program was run. So it is obvious that calling the &amp;quot;clock&amp;quot; function in a later time would give a higher value. So subtracting the earlier value from the later one should give a positive number.&lt;br /&gt;
&lt;br /&gt;
Overall my approach was the obvious way of timing something. But if I had gone into more depth I could have gotten a better way of doing it.&lt;br /&gt;
&lt;br /&gt;
Problems:&lt;br /&gt;
* I think that neglecting the time taken for doing the comparison operation was a bad idea since the comparison operation is also done the same amount of times as the addition operation. So it might affect my final results significantly.&lt;br /&gt;
&lt;br /&gt;
Improvements:&lt;br /&gt;
* I think a better way of doing it would have been to time the execution of the function itself WITHOUT the addition operation so then I can subtract that result from the one I get when I time the function WITH the addition operation. Since I use the loop incrementation part in the &amp;quot;for&amp;quot; loop as the addition operation I need to add another addition operation inside the for loop if I use this new method.&lt;/div&gt;</summary>
		<author><name>Mark</name></author>
	</entry>
</feed>