Wednesday, February 9, 2011

Counting in Labs (For hardcore programmers!!!)

The below experiments were made in our labs, in order to count the time needed for a task to be completed in several ways.
We used an Ubuntu 10.10 linux machine, but it can be probably done in every linux distribution.

The target of our experiment was to watch the time needed for a simple operation to complete in several ways. Each unit was counted ten times using the script we created:

time for ((i=1; i<=10;i++)); do
java Addition $i $i
done

That way the Addition program will be executed 10 times. We divide the final output by 10 so we have a representation of the time taken to complete an operation.

We also created in java the program addition. We wanted to check first the use of functions, so we created two versions of the program, and got the results written below of each:

public class Addition
{
public static void main(String[] args)
{
int a = Integer.parseInt(args[0]);
int b = Integer.parseInt(args[1]);
System.out.println(add(a,b));
}
private static int add(int x, int y)
{
return x + y;
}
}

real 0m0.872s
user 0m0.584s
sys 0m0.228s

divide each by 10
real 0m0.087s
user 0m0.058s
sys 0m0.023s
------------------------------------------------------
public class Addition
{
public static void main(String[] args)
{
int a = Integer.parseInt(args[0]);
int b = Integer.parseInt(args[1]);

System.out.println(a + b);
}
}

real 0m0.870s
user 0m0.584s
sys 0m0.224s


real 0m0.087s
user 0m0.058s
sys 0m0.22s

Not much a difference. But we know that printing results out costs lots of resources.
So we try again (just to be sure) with these two:

public class Addition
{
public static void main(String[] args)
{
int a = Integer.parseInt(args[0]);
int b = Integer.parseInt(args[1]);
int c = add(a,b);
}
private static int add(int x, int y)
{
return x + y;
}
}

real 0m0.869s
user 0m0.556s
sys 0m0.208s

divide by 10
real 0m0.087s
user 0m0.0556s
sys 0m0.021s
The first thing we see is that printing in the system output cost's indeed lot of time for the system (look at the sys times). The user time also is smaller. Real time is not much of a difference, however note that small differences are still important in computers.

------------------------------------------------------------------------------------------------------
public class Addition
{
public static void main(String[] args)
{
int a = Integer.parseInt(args[0]);
int b = Integer.parseInt(args[1]);
int c = a + b;

}
}

real 0m0.864s
user 0m0.604s
sys 0m0.192s

divide by 10
real 0m0.086s
user 0m0.060s
sys 0m0.019s
--------------------------------------------
Conclusions until now:
-In java functions take longer to output results.
-Printing out results takes lot of time

Send as your experiments and conclusions at: vaslabsco@gmail.com

No comments:

Post a Comment