I'm testing this using a profiling object I wrote. I have tested it and know that it is returning reasonable data. So what the program is doing is running a loop over a string and checking the time of the loop itself, and the loop body.
Code: Select all
import utime
import perf
if __name__ == '__main__':
text = 'The quick brown fox jumps over the lazy dog'
perf_sampler = perf.HistogramSampler()
loop_sampler = perf.HistogramSampler()
perf_updated = False
while True:
loop_sampler.begin()
for c in text: # <- The overhead is being calculated per iteration here
# It is the difference between the iteration time and the body time
perf_sampler.begin()
utime.sleep_us(10)
perf_sampler.end()
perf_updated = loop_sampler.end()
if perf_updated:
overhead_us = ((loop_sampler.data.mean * loop_sampler.data.samples) - (perf_sampler.data.mean * perf_sampler.data.samples) ) / perf_sampler.data.samples
print('Character Time = {} us / character'.format(loop_sampler.data.mean / len(text)))
print('Character Body Time = {} us / character'.format(perf_sampler.data.mean))
print('Character Loop Overhead = {} us / character'.format(overhead_us))
print()
So, the question is, is there a way for me to optimize this? I would even be willing to write the module in C/C++ if needed, but I'm not actually sure how to check where the time is being spent in the iteration. Since the strings are being formatted at runtime, I'm going to have actual strings in Python... unless I just go to C++ for the whole project (which I'd rather not do, as I went to micropython because I'd like to allow scripting on the system).
Any ideas are always greatly appreciated.
Cheers.