Home
JAQForum Ver 20.06
Log In or Join  
Active Topics
Local Time 22:54 27 Apr 2024 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 : Optimising Code for Speed

Author Message
marcwolf

Senior Member

Joined: 08/06/2009
Location: Australia
Posts: 119
Posted: 02:51pm 07 May 2013
Copy link to clipboard 
Print this post

HI.
I have a routine that I'd like to monitor for speed to see if it can be made to run faster.
I have put the routine into a FUNCTION for ease of calling

Since MMBasic is an interpreted language are their any tricks that can help. i.e. multiple statements per line, reduced whitespace etc.


Function test1$(recnum, names$)
Local loop1
Local File1$
File1$ = "a:\" + names$ + ".txt"
Option error continue
Open File1$ For input As #1
If MM.Errno = 6 Then
test1$ = "FNF"
Exit Function
EndIf
For loop1 = 1 To (recnum -1)
Line Input #1, test1$
If Eof(#1) Then
test1$ = "EOF"
Exit Function
EndIf
Next
Print test1$
Input #1, bb$,cc$
Print "rec " + bb$
test1$ = cc$
Close #1

End Function


Essentially it is a go to record XXX routine. I need to open the file each time so to reset the record pointer as there is no SEEK command.
But as this will be called a lot I would like it to run as fast a possible

many thanks
Dave
Coding Coding Coding..
Keep those keyboards coding..
RAW CODE!!!!!
 
crackerjack

Senior Member

Joined: 11/07/2011
Location: Australia
Posts: 164
Posted: 01:32am 08 May 2013
Copy link to clipboard 
Print this post

Any chance you could read this file into memory (array) instead of opening and sequentially seeking?
If I am way off please forgive me. I have not coded in MMBasic for a versions now but keen to get back to it.
 
MicroBlocks

Guru

Joined: 12/05/2012
Location: Thailand
Posts: 2209
Posted: 03:08am 08 May 2013
Copy link to clipboard 
Print this post

Some type of read ahead buffer (into an array) could be used.
It is very depended on the sequence the records are read.
As the part of reading from a file is the slowest any gain by using 'tricks' would probably only gain you a fraction of a percent.

If speed is essential, you could use an external serial ram/rom/flash device and directly access it with spi or i2c. You would then be able to use a seek and really improve performance.

Another would be to modify the MMBasic interpreter and add a seek command. :)


Microblocks. Build with logic.
 
Geoffg

Guru

Joined: 06/06/2011
Location: Australia
Posts: 3165
Posted: 04:23am 08 May 2013
Copy link to clipboard 
Print this post

Actually, the seek command should be in the language anyway, so I plan to include it in the next version.

Geoff
Geoff Graham - http://geoffg.net
 
marcwolf

Senior Member

Joined: 08/06/2009
Location: Australia
Posts: 119
Posted: 12:45pm 08 May 2013
Copy link to clipboard 
Print this post

  TZAdvantage said   Some type of read ahead buffer (into an array) could be used.
It is very depended on the sequence the records are read.
As the part of reading from a file is the slowest any gain by using 'tricks' would probably only gain you a fraction of a percent.

If speed is essential, you could use an external serial ram/rom/flash device and directly access it with spi or i2c. You would then be able to use a seek and really improve performance.

Another would be to modify the MMBasic interpreter and add a seek command. :)



Thanks for the input. The file will be sequences of servo commands and as there will be several of the files it would not be easy to bring them all into memory. Also - as we are working with flash drives (SD Cards or internal A drive) the difference in speed between i2c/spi and the card would be very small.

Coding Coding Coding..
Keep those keyboards coding..
RAW CODE!!!!!
 
marcwolf

Senior Member

Joined: 08/06/2009
Location: Australia
Posts: 119
Posted: 12:47pm 08 May 2013
Copy link to clipboard 
Print this post

  Geoffg said   Actually, the seek command should be in the language anyway, so I plan to include it in the next version.

Geoff


That is great news Geoff - Random access at last.
So you have any idea of a timeline as I have been waiting for this for a while

Dave
Coding Coding Coding..
Keep those keyboards coding..
RAW CODE!!!!!
 
Geoffg

Guru

Joined: 06/06/2011
Location: Australia
Posts: 3165
Posted: 03:26am 09 May 2013
Copy link to clipboard 
Print this post

  marcwolf said  That is great news Geoff - Random access at last.
So you have any idea of a timeline as I have been waiting for this for a while
Dave

At this time I am aiming for 4 to 6 weeks.
Geoff Graham - http://geoffg.net
 
James_From_Canb

Senior Member

Joined: 19/06/2011
Location: Australia
Posts: 265
Posted: 12:59am 10 May 2013
Copy link to clipboard 
Print this post

If the physical reads are slow, maybe you could pack multiple records into each file line then split the record you want out of the packed line.
For example, if your records were each 64 bytes long, you could pack four records onto each line, just as long as you don't exceed 256 bytes for the entire line. You only have to make 25% of the physical reads to get to the data you want.
That will become redundant once MMBasic gets a seek command, but it's an interesting question in the meantime.

James
My mind is aglow with whirling, transient nodes of thought careening through a cosmic vapor of invention.

Hedley Lamarr, Blazing Saddles (1974)
 
shoebuckle
Senior Member

Joined: 21/01/2012
Location: Australia
Posts: 189
Posted: 03:09pm 11 May 2013
Copy link to clipboard 
Print this post

Removing comments, indents and blank lines and changing long variable, label, function and subroutine names to 1 or 2 character names can make a significant difference to execution speed. I ran my almost complete Crunch program against Alphanum.bas and it reduced program file size from 14KB or 5KB and execution against a typical text file from 76 secs to 56 secs on a Mono Maximite, an improvement of 26%.

I have one more function to complete in the Crunch suite of programs before letting others have a play. The beta version should be available in the next couple of days.

Cheers,
HughEdited by shoebuckle 2013-05-13
 
crackerjack

Senior Member

Joined: 11/07/2011
Location: Australia
Posts: 164
Posted: 06:53pm 12 May 2013
Copy link to clipboard 
Print this post

You could try encoding the Servo commands - no idea what your input actually looks like or the application/servo resolution required, but you have a range of about 95 steps using single (printable) ASCII characters for encoding... That is if ASCII character 32 " " (SPACE) represents the bottom the range and character 126 "~" the top of the range. This could be mapped to servo pulse values of 1.0 ms to 1.95 ms (or whatever your application/servos require).

Of course the program would need to translate the characters using ASC() to pulse widths (plus any offset)... etc.

Using this compressed approach to the data may allow you to load all the data into memory at once (with some careful management of the sequences).

Or you could wait a few weeks for a SEEK command. Edited by crackerjack 2013-05-14
 
marcwolf

Senior Member

Joined: 08/06/2009
Location: Australia
Posts: 119
Posted: 07:07pm 12 May 2013
Copy link to clipboard 
Print this post

HI Folks.
Many thanks for all of the advice.
I'll wait for the Seek command as there should be some additional commands for handling random access files.

I have already made my Record-Decode routines that work well, it was mainly the seek command for when I need to jump forward or back through the sequencing (also handled by a command structure within the record format)

Shoebuckle - Thanks for the specific info re the code optimisations. This was more what I was looking for however it did get the result of finding that a see command was in the process.

Many thanks all
Marc
Coding Coding Coding..
Keep those keyboards coding..
RAW CODE!!!!!
 
Print this page


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

© JAQ Software 2024