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: AustraliaPosts: 119 |
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: AustraliaPosts: 164 |
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: ThailandPosts: 2209 |
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: AustraliaPosts: 3194 |
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: AustraliaPosts: 119 |
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: AustraliaPosts: 119 |
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: AustraliaPosts: 3194 |
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: AustraliaPosts: 265 |
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: AustraliaPosts: 189 |
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, Hugh |
||||
crackerjack Senior Member Joined: 11/07/2011 Location: AustraliaPosts: 164 |
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. |
||||
marcwolf Senior Member Joined: 08/06/2009 Location: AustraliaPosts: 119 |
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 |