Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 07:03 12 Nov 2025 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : Micromite trig functions cached?

Author Message
factotum
Newbie

Joined: 29/10/2017
Location: United Kingdom
Posts: 18
Posted: 03:03pm 26 Oct 2018
Copy link to clipboard 
Print this post

I've been timing some Micromite Basic functions of mine that are very heavy on calling sin, cos, tan etc. I am surprised how fast they are. One function that calls a lot of trig functions only takes 42 ms at 10 MHz clock.

Although I have made some effort to call them with a variety of different arguments within the timing loop, I just wondered if the trig library might contain some caching mechanism that remembers recently-requested argument values and returns the result quickly, rather than re-cacluating it from scratch.

Anyone know?Edited by factotum 2018-10-28
 
Paul_L
Guru

Joined: 03/03/2016
Location: United States
Posts: 769
Posted: 04:20pm 26 Oct 2018
Copy link to clipboard 
Print this post

It's very simple to test it.

t1=timer
for j=1 to 100
for i=0.1 to 1.5 step 0.1
A=sin(i) '1500 calls to sin with different values
next i
next j
t2=timer-t1:print t2
t3=timer
for j=1 to 1500
A=sin(1) '1500 calls to sin with the same value
next j
t4=timer-t3:print t4

Paul in NY
 
JohnS
Guru

Joined: 18/11/2011
Location: United Kingdom
Posts: 4133
Posted: 07:04pm 26 Oct 2018
Copy link to clipboard 
Print this post

That might fail as a test. E.g. if some values take longer to calculate than others.

Is reading the source code not an option?

I think it's more a case of the CPU being fast :)

JohnEdited by JohnS 2018-10-28
 
CaptainBoing

Guru

Joined: 07/09/2016
Location: United Kingdom
Posts: 2171
Posted: 08:42pm 26 Oct 2018
Copy link to clipboard 
Print this post

  factotum said   I've been timing some Micromite Basic functions of mine that are very heavy on calling sin, cos, tan etc. I am surprised how fast they are. One function that calls a lot of trig functions only takes 42 ms at 10 MHz clock.

Although I have made some effort to call them with a variety of different arguments within the timing loop, I just wondered if the trig library might contain some caching mechanism that remembers recently-requested argument values and returns the result quickly, rather than re-calculating it from scratch.


I doubt it - that is a very broad broom with no real reason to expect you'd be wanting the same answer.

I would imagine the transcendental functions are a product of the C compiler (and it's libraries) which most likely uses Polynomial functions to calculate the values. The time to cache and then search that cache is just too hit n miss, and wasted effort if a suitable match isn't found... just get down to the business of calculating the value.

Polynomials of short sequences are very fast - I coded several in my early 20's to convert frequencies from a sonde receiver into real-life pressure, temperature & humidity values for PILOT/TEMP messages for the met office. Even on a 4MHz Z80 running interpreted basic they were over in the twinkle of an eye.

Edited by CaptainBoing 2018-10-28
 
Geoffg

Guru

Joined: 06/06/2011
Location: Australia
Posts: 3308
Posted: 10:08pm 26 Oct 2018
Copy link to clipboard 
Print this post

No, there is no caching.

In MMBasic SIN(), TAN(), etc are just wrappers around floating point library functions provided by Microchip with their C compilers for the PIC32 family of chips. Microchip boast that these functions are carefully tuned for speed and they are fast.

Another factor is the PIC32 chip with its MIPS processor core - it is very fast. In the 90's a number of million dollar minicomputers (Tandem, Pyramid) were built around the MIPS processor and now you can get the same technology in a $10 chip. The rate of change is amazing.

GeoffEdited by Geoffg 2018-10-28
Geoff Graham - http://geoffg.net
 
factotum
Newbie

Joined: 29/10/2017
Location: United Kingdom
Posts: 18
Posted: 03:11pm 30 Nov 2018
Copy link to clipboard 
Print this post

Thanks for the above responses. Sorry I didn't get back sooner.
 
Print this page


To reply to this topic, you need to log in.

The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025