In the experiments in the previous section, we concluded that the "non-Gauss" block had a linear runtime, while the "Gauss" block had a constant runtime.
Both blocks were answering the same question: I have a list of numbers from 1
to max
,
and I want to know what the total of these numbers is. The
"non-Gauss" block is a great first attempt at solving the problem, and
works rather fast even for large lists. However, its main
disadvantage is that its runtime is nonetheless dependent on the length
of the input, and so the longer the list, the longer you will have to
wait to get an answer. The "Gauss" block, however, does not have
to go through the numbers in the list; all it ever needs, every time,
is one piece of information: the length of the list, which (in this
case) is also the value of max
. Once you give it this
information, it will always perform the same number of arithmetic
operations, regardless of whether the list is 10 elements long or 10
million, and that instantly makes it the better algorithm.
Many computer scientists work every day to try and get this kind of an improvement in running time, or to try and show that you can't get any faster.