Menu

Math Test

Chris S
2020-08-27
2020-08-31
  • Chris S

    Chris S - 2020-08-27

    Hello everyone! I couldnt find a thread on if anyone has done this before, so I ran my own tests to see how quickly GCB/PIC can run through very large numbers (words and longs). I had initially started with random numbers to make sure I didnt fudge the test results, but came to the conclusion quickly that in doing so, I might get very large results, larger than what a PIC can handle. So in the end, I used semi-realistic numbers that I know would work.

    The reason why I chose to do this test is because I want to make an electronic lead screw for my lathe, so I want to make sure it can do the math in enough time and send a pulse out to move a stepper while the main spindle is running. I wanted to make sure a 8 Bit micro can do it in enough time, or else I would have to learn 32 Bit Micro's and find a comparible language to GCB.

    Complier:
    0.98.07 RC20 2020-07-14 (Windows 64 bit)
    PIC: 18F25K20
    16384 Words of Program Memory
    1536 Bytes of Ram

    I tested it through 4 main speeds, 8, 16,32 and 64 Mhz. By time I got to the multplication tests, that was when I realized its better off just testing at 64Mhz, since if you are doing something quick, you will most likely use that speed.

    When I selected numbers, I made sure I got the correct result by having it be spit out via UART. I then commented the UART lines back in and just observed a port going High/Low. Since it was toggling, both speeds are roughly the same.

    End result: Worst case test was division that took 69uS @64 Mhz. It seems that trying to keep things as words is most efficient, unless you need the large numbers that long's can give.

    Note: I never tested Integers. Opps.

    I cant say for certain if I tested things "correctly" but this will give a ball park figure. Maybe I can improve my testing method as its possible I did something incorrect.

    Results below:

    All vars were LONG for addition test
    Vars were random for this test
    
    Additon takes 9.6uS @ 8Mhz
    Additon takes 4.8uS @ 16Mhz
    Additon takes 2.8 uS @ 32Mhz
    Additon takes 1.44 uS @ 64Mhz
    
    Alpha=541180911
    Beta=5
    
    Multiplication takes 116 uS @ 8Mhz
    Multiplication takes 60 uS @ 16Mhz
    Multiplication takes 30 uS @ 32Mhz
    Multiplication takes 15 uS @ 64Mhz
    
    Vars below were all LONG
    
    Alpha=5410911
    Beta=930
    Multiplication takes 39 uS @ 64Mhz
    
    Alpha=5410911
    Beta=260
    Multiplication takes 34 uS @ 64Mhz
    
    Alpha=54109511
    Beta=2
    Division takes 64 uS @ 64Mhz
    
    Alpha=54109511
    Beta=2
    Division takes 64 uS @ 64Mhz
    
    Alpha=655300
    Beta=31415
    Division takes 69 uS @ 64Mhz
    
    Word*word=long test
    Alpha=65530
    Beta=3141
    Multiplication takes 45 uS @ 64Mhz
    
    
    Word Test
    Alpha=65530
    Beta=3
    Division takes 21 uS @ 64Mhz
    
    Alpha=65530
    Beta=3141
    Division takes 23 uS @ 64Mhz
    
     
  • SPostma

    SPostma - 2020-08-31

    Hi Chris,
    a division by a constant value often can be rewritten to a constant multiplication like (2^16 / divisor), followed by a right shift (or taking the high word as result).

    So instead of Word Test
    Alpha=65530; Beta=3; result = Alpha / Beta
    write
    Alpha=65530; Beta=65536/3; result = (Alpha * Beta) shr 16

    A multiplication is many times faster than a division,
    especially if the processor has a hardware multiplicator.

     

Log in to post a comment.