Software efficiency lab: some elements of response#

Experimental conditions

The results shown here have been obtained with a HP EliteBook laptop. You can obtain different results on the computer with which you have done the experiments.

What is the most efficient sorting algorithm#

Among slow algorithms selectionSort, bubbleSort, insertionSort, and quick algorithms quickSort, mergeSort, mergeSort has been the most efficient during our experiments. It has been the only sorting algorithm able to sort 43238 lines of order details in a reasonable time.

mergeSort is the algorithm that has the lowest Worst case time complexity \( O(nlog(n))\) instead of \(O(n^{2})\) for the other algorithms.

Some slow algorithms have good results with small tables (e.g. bubble sort, insertion sort). But with large tables, their efficiency drops suddenly at some inflection point.

How to extract data from a database and write them in a file efficiently?#

While writing in a file, writing by character, column or row does not seem to have a negative impact. System calls that write in the file are certainly optimized.

But we found a good optimization with the following modification in a loop on the linked list.

  • To iterate over a linked list, favor the enhanced for-each structure instead of a loop with a get(i) call. Indeed, we have learned in the discover Joular JX lab that accessing elements in a LinkedList is expensive.

Avoid

for (int i = 0; i < rows.size(); i++) {
	final List<String> row = rows.get(i);
	String line = buildString(row);
	writeInFile(csvOutputFile, line);
}

Favor

for (final List<String> row : rows) {
	final String line = buildString(row);
	writeInFile(csvOutputFile, line);
}

In conclusion, each loop should be written carrefully !

How do SQL queries perfom compared to a Java program that reads from a local file?#

  • When reading a file, it is more efficient to read line by line rather than character by character.

  • On the laptop used for the experiment, reading on a local file was more efficient than using the database server, even when the sort has been done locally. The cost of sending the request and receiving the data from the server was more costly than a local sort.

How efficient is SQL for complex queries?#

We have been able to:

  • Reduce the number of the connections to the database

  • Reduce the number of queries done by the database while writing a complex query, by having the work done by the database server, we have been able to reduce the energy consumption on the database client side.

A list of (at least three) rules that you have learned from this lab to gain energy in software#

  1. While choosing an algorithm, take the best one in terms of complexity. Even if an algorithm seems better for short tables, it is important to take care to the worst case complexity. While writing an algorithm, always minimize the complexity.

  2. To iterate over a linked list favor the enhanced for-each structure, instead of a loop with a get(i) call (cf. Discover Joular JX lab).

  3. If we want to generalize, each loop should be written carefully !

  4. Local read is better than a remote database server call.

  5. You should reduce the number of queries to a database: make the job done by the database server ! That is normally optimized for it !

  6. Write the code carefully for better efficiency.