Calculating .5*x and x/2

For discussions about programming, programming questions/advice, and projects that don't really have anything to do with Puppy.
Message
Author
User avatar
mahaju
Posts: 487
Joined: Mon 11 Oct 2010, 07:11
Location: between the keyboard and the chair

Calculating .5*x and x/2

#1 Post by mahaju »

I heard that a computer does the operation .5*x faster than x/2. Is this true? Can you please tell me why or how this works?
Thank you.

jpeps
Posts: 3179
Joined: Sat 31 May 2008, 19:00

#2 Post by jpeps »

I tried with my perl calculator, and the reverse was true

User avatar
mahaju
Posts: 487
Joined: Mon 11 Oct 2010, 07:11
Location: between the keyboard and the chair

#3 Post by mahaju »

I am talking from the perspective of the workings of a general microprocessor, perhaps with no OS or compilers
In general, is it true that a microprocessor calculates .5*x faster than x/2, or does it depend on the presence of some kind of floating point processor in the system?

User avatar
Flash
Official Dog Handler
Posts: 13071
Joined: Wed 04 May 2005, 16:04
Location: Arizona USA

#4 Post by Flash »

If I remember right, if x is a binary number, all you have to do to divide it (or multiply it) by two is move the decimal point over one place.

User avatar
mahaju
Posts: 487
Joined: Mon 11 Oct 2010, 07:11
Location: between the keyboard and the chair

#5 Post by mahaju »

multiplying by 0.5 and dividing by 2 are essentially the same mathematical operations, however, someone told me that multiplication by 0.5 is done faster by a computer than a division by 2. I need to know what is the reason for that.

User avatar
Flash
Official Dog Handler
Posts: 13071
Joined: Wed 04 May 2005, 16:04
Location: Arizona USA

#6 Post by Flash »

I think you're getting confused by talking about binary arithmetic using decimal numbers and the shortcuts that have been developed to multiply and divide them on a blackboard.

Anyway, I suppose that how fast a computer takes to multiply and divide, especially by two, depends on the algorithm used and how good the programmer was. In other words, you'll have to look at the code to find the answer to your question.
Last edited by Flash on Thu 15 Sep 2011, 03:30, edited 1 time in total.

User avatar
mahaju
Posts: 487
Joined: Mon 11 Oct 2010, 07:11
Location: between the keyboard and the chair

#7 Post by mahaju »

I am not confused about whether .5 * x is same as x/2.
My main question is why does a microprocessor calculate 0.5*x faster than x/2, when both are the same mathematical operation?

User avatar
Flash
Official Dog Handler
Posts: 13071
Joined: Wed 04 May 2005, 16:04
Location: Arizona USA

#8 Post by Flash »

Sorry, I edited my post to remove a badly thought-out part while you were replying. :oops:
Have you confirmed that it actually does take longer one way than the other?

User avatar
mahaju
Posts: 487
Joined: Mon 11 Oct 2010, 07:11
Location: between the keyboard and the chair

#9 Post by mahaju »

actually it's just something my friend told me
I am talking about the process of multiplication and division as done by a microprocessor at the machine level
perhaps my friend was talking with reference to MUL and DIV instruction as used in the 8086 microprocessor

Bruce B

#10 Post by Bruce B »

In my quick test, I think the weak link in the speed chain is monitor display time.

Code: Select all

#include <stdio.h>

int main() {

double  x = 987654321;


	printf("%f\n", x*.5);
	printf("%f\n", .5*x);
	printf("%f\n", x/2);


}
binary and source attached

~
Attachments
fun-test.zip
(2.48 KiB) Downloaded 170 times

Bruce B

#11 Post by Bruce B »

Flash,

If you look at my code you will see I'm using decimal figures. This is for human readability.

However, I think the processor only deals with 0s and 1s and if so, all computations must be made in binary.

Bruce

~

User avatar
Flash
Official Dog Handler
Posts: 13071
Joined: Wed 04 May 2005, 16:04
Location: Arizona USA

#12 Post by Flash »

Correct. Somewhere along the line has to be a hidden step where the decimal number is converted to binary. Maybe this is done by means of an algorithm that was perfected decades ago, but it still has to be there.

muggins
Posts: 6724
Joined: Fri 20 Jan 2006, 10:44
Location: hobart

#13 Post by muggins »

Mahaju,

surely the easiest solution is to ask your friend where they sourced this info?

User avatar
mahaju
Posts: 487
Joined: Mon 11 Oct 2010, 07:11
Location: between the keyboard and the chair

#14 Post by mahaju »

you're right
but it will have to wait, I can't ask him immediately
and in the mean time I can ask here in the forum much quicker :P
@Flash: That is related to the C compiler design

User avatar
Dave_G
Posts: 453
Joined: Thu 21 Jul 2011, 13:53

#15 Post by Dave_G »

Keep in mind that no matter what programing language you are using,
once compiled it will run as machine code on the CPU.
Assuming a X86 CPU, factors such as, is the number a 8,16,or 32 bit one
and is it in memory or preloaded into a register all make a difference.

e.g.

MOV ax, 0x400
MOV bx, 0x002
MUL bx ;(or DIV, answer now in ax)

According to the Intel specs, a MUL takes on average 15 "clocks"
where as a DIV takes on average 16.
I say average because it depends on number of bits and if in regs or mem.

But all this is really academic since the time taken by all the other functions
and routines in the rest of the high level C program will take much much longer then the
slight difference between a MUL and a DIV.

So unless you happen to be doing millions and millions of DIV or MUL operations, you will not notice any advantage of one method over the other.

EDIT:

If you want to try it for yourself compile these two short pgms with NASM
then use 'strace -t' to get an aproximation of execute time take.
Please note that neither pgm gives any output to the screen, it's simply
used to get an idea of computation time taken for a DIV or MUL:

Here is the one for MUL, save it as mul.asm then compile.

Code: Select all

section .text
 global _start 

_start:

mov eax, 400
mov ebx, 2
mul ebx 

mov eax,1 ;system call number (sys_exit)
int 0x80 ;call kernel
Here is the one for DIV, save it as div.asm then compile.

Code: Select all

section .text
 global _start 

_start:

mov eax, 400
mov ebx, 2
div ebx 

mov eax,1 ;system call number (sys_exit)
int 0x80 ;call kernel
This way you have no high level stuff slowing down the pgm.

ANOTHER EDIT:

Remember that a lot of these "myths" about code optimization techniques
hark back to the days of the 386 (and before) where certain combinations
of op-codes would yield a slightly faster execution time.
The CPUs of today all have pre-fetch buffers and other code optimization
techniques built in and run at such high speeds compared to their "ancient"
cousins that it's simply not worth worrying about.

Another popular one was when initializing a variable to zero, it was faster
to do a XOR eax, eax rather then a MOV eax, 0

Dave.

User avatar
L18L
Posts: 3479
Joined: Sat 19 Jun 2010, 18:56
Location: www.eussenheim.de/

#16 Post by L18L »

my 2 cents
dividing could have code added to test on zero 8)

User avatar
mahaju
Posts: 487
Joined: Mon 11 Oct 2010, 07:11
Location: between the keyboard and the chair

#17 Post by mahaju »

I thought at the machine level such kind of test happened on hardware and not using code, for example, if division by zero is done some kind of overflow flag bit is set in the microprocessor's PSW register

User avatar
L18L
Posts: 3479
Joined: Sat 19 Jun 2010, 18:56
Location: www.eussenheim.de/

#18 Post by L18L »

mahaju wrote:I thought at the machine level such kind of test happened on hardware and not using code, for example, if division by zero is done some kind of overflow flag bit is set in the microprocessor's PSW register
Setting this bit might explain the difference?
Dave_G wrote:According to the Intel specs, a MUL takes on average 15 "clocks"
where as a DIV takes on average 16.

User avatar
mahaju
Posts: 487
Joined: Mon 11 Oct 2010, 07:11
Location: between the keyboard and the chair

#19 Post by mahaju »

but it's set only if division by zero is encountered not always
for example, if result of the division exceeds the maximum capacity of the register an overflow bit may be set

User avatar
Dave_G
Posts: 453
Joined: Thu 21 Jul 2011, 13:53

#20 Post by Dave_G »

Note that the CPU does not do a check for dividing (or multiplying) by zero
before it does the actual operation.
Like wise if an overflow occurs.
If it does encounter these conditions then the flag is set.

@mahaju,
The flags register in the x86 range is not called the PSW register, are you
sure you are referring to the same CPU and not some microprocessor such
as the PIC or AVR?

Dave.

Post Reply