r/AskComputerScience • • Apr 20 '21

Does a computer take the same amount of time to divide 1024 by 2 as it would to divide 256 by 4?

I am trying to understand how the splitting part of merge sort runs in O(log2(n)) time as it has to go through hundreds of divisions in later lines. Surely 1024/2 will complete a lot faster than 2/2, 512 times would, right?

Thanks!

32 Upvotes

12 comments sorted by

4

u/[deleted] Apr 21 '21

This really dependent on the the computer, and how it's perform division.

For example if you're using the fast multiplication algorithm like Harvey-Hoeven algorithm, it has nlogn complexity where n is the highest digits of the multiplication.

A fast division algorithm is using a multiplication algorithm also, let's use Newton–Raphson division and Harvey-Hoeven algorithm then the complexity would be:

n^2logn

again, this is using the most digits of the algorithm, so basically 1024/2 is around 16 times longer than 2/2.

However, please note that this is general problem, this doesn't work in the real world, why? because most language has maximum and minimum value, C for example has different maximum value of ints, (short int, int, long int, etc). You can theoretically create an algorithm using the same amount of power to computer all of it's value.

Another implementation is storing 1/MIN_INT to 1/MAX_INT, so if you want to calculate x/y, you just need to look up 1/y then multiply the answer by x.

2

u/thenonovirus Apr 21 '21

Thank you for the explanation!

As for the merge sort, I was under the impression that the dividing part of merge sort was O(log(N)) because the depth of divisions (apologies if this is the incorrect terminology) is equal to that of log2(n). For example, log2(1024) == 10.

Then the merging part was o(n) for each row because each row has to be iterated through fully, which when multiplied by O(log(n)), would give the time complexity of O(n log2(n)).

1

u/S-S-R Apr 21 '21

Harvey-Hoeven is galactic though. Unless you are using bigint, any operation within the [0;2^64] will run at the same speed as it is all hardware.

28

u/DrunkHacker Apr 21 '21 edited Apr 21 '21

Both are bit shift operations.

It would depend on the architecture and whether it supports shifting by more than one bit at a time.

38

u/chrisgseaton Apr 21 '21

Surely 1024/2 will complete a lot faster than 2/2, 512 times would, right?

On the machines you're typically using, integer division is a mostly constant-time operation, so any division runs about as fast as any other division.

6

u/Ultimegede Apr 21 '21

That question is not easy to answer. You will have to look at the instruction level of the machine code to figure that out. I imagine they are probably equally fast.

4

u/[deleted] Apr 21 '21

we write numbers with varying amount of digits, but 1024, 2, 256, 4 are all represented with the same number of bits. its kinda like if we wrote it as 1024,0002,0256,0004.
as a result time is fairly constant.

6

u/jhaluska Apr 21 '21

Your subject question is worded oddly/vaguely. So I'll try to answer them all. For integer division, the answer is Yes and No. It's faster on a transistor level, but constant on an instruction level.* (Unless it's hard coded power of two dividing an integer which might even be the same speed at the transistor level.)

But you're really asking about splitting/merge in relation to sorting. The "splitting" part really isn't log2(n) it's the merging part.

But to get to the heart of what I think is your question, there are a lot of sorting algorithms of the same runtime complexity but the real world performance will be slightly different on different hardware. It's one of the reasons we don't have a "single" sorting algorithm that everybody uses.

Since sorting is so common, some libraries will actually spend a lot of time optimizing them and will even implement two or more algorithms based off the number of elements.

3

u/SploogeLoogie Apr 21 '21

In terms of big-O, I think this is a really good observation that the cost of dividing the dataset itself may not be constant (depending on cpu implementation of the DIV instruction). If you thought of that yourself, good job. I would address it by saying that even if N*N / 2 does take longer than N/2, it's not by an unbounded amount and maybe just by a few cycles more. Since the data set is reducing in size exponentially, its really not a factor. But this could raise issues if you were dealing with a dataset not in memory. Like a distributed computer or a database, where "dividing it in half" might be a N or N2 expensive operation. This could be a situation were binary search is faster in theory but slower in practice.

2

u/Felicia_Svilling Apr 21 '21

Usually when measuring the complexity of sorting algorithms, you count the time in the number of comparisons that you need to make, and don't go into details of how long time division will take or such. (this fails for radix sort though).

Anyway, as long as your number fits in a single register the operation is going to take just as much time, since there is a fixed circuit for it. In theory complexity analysis should work for all sizes of n, even those that doesn't fit into a register, and then a CPU is going to need more time for larger numbers. But on the other hand you also will run into n so big that you can't fit it in the memory of the computer.

This is why you don't do complexity analysis in terms of actual computers. Real computers are limited, and complexity analysis is not.

1

u/S-S-R Apr 21 '21

It all runs through the ALU which has effectively constant time for all operations as long as they are in the range of 0..2^64. I.e all divisions cost the same, all adds cost the same, however add is cheaper than division and bitshift ( like 1024/2) is the fastest.

1

u/claytonkb Apr 22 '21

add is cheaper than division and bitshift ( like 1024/2) is the fastest.

On modern CPU architectures, ADD is a single-cycle operation like SHL/SHR/ASHR (bit-shifts), so a bit-shift is not faster. However, a bit-shift will probably use less power than an ADD (especially if the addition involves a lot of carries), so the power-consumption of CPU running a long stream of ADD instructions will be higher than for the same CPU running a long stream of SHx instructions, if that makes sense. Note that this difference would be very small, so I don't think it would be measurable at the pins, you would have to be able to probe inside the CPU to measure it.