Popup YouTube Video
Sheerpower Logo

Built-in Code Profiler


Sheerpower's built-in profiler generates a report showing the execution time and frequency of each routine. By identifying the routines with the longest execution times, you can focus your optimization efforts where they will have the greatest impact on performance. Sheerpower includes a built-in code profiler. It is used for:
  • Identifying Bottlenecks: Pinpoints slow or inefficient parts of the code.
  • Optimizing Performance: Helps make data-driven decisions to improve code efficiency.
  • Providing Detailed Insights: Provides comprehensive statistics on execution times and resource usage.
  • Improving Debugging: Works alongside debugging tools to provide a complete picture of code performance.
  • Producing Cost-Effective Development: Reduces time spent on trial-and-error optimizations, streamlining the development process.

To activate it, add these statements to the top of your code:
debug on stats on
Then, after you have run your code long enough or just before your program ends:
list stats
This writes profiling information to a file named myprogram_stats.txt where myprogram is the name of your program. The statistics file contains the names of every routine that is called. For each routine the amount of elapsed time spent in that routine and a count is provided. The count is the maximum number of times any statement within the routine was executed.

At the end of the file is a list of the top 20 routines that took the longest elapsed time to execute. This information lets you focus on which routines need to be optimized the most.

Note: Sheerpower’s built-in profiler is an instrumented, source-mode routine timer. It reports accumulated elapsed time and execution counts to help identify candidate hotspots. Results may include waiting time and profiling overhead, so expensive routines should be investigated further before being optimized.
debug on stats on for idx = 1 to 10 more_work do_it do_it2 next idx list stats stats off debug off stop routine more_work x=x+1 delay .05 end routine routine do_it2 delay .3 x=x+1 end routine routine do_it delay .2 x=x+1 end routine stop

Below is the profile as generated:
Sheerpower Profiling Statistics 07-SEP-2022 08:02:25 C:\Sheerpower\stats.spsrc 0.62 10 routine more_work 2.02 10 routine do_it2 2.00 10 routine do_it ***** Longest Executing Routines ***** 2.02 10 routine do_it2 2.00 10 routine do_it 0.62 10 routine more_work ************************ --- End of stats ---

Note: You will need to download Sheerpower to try the profiling feature.
In addition, individual blocks of code can be timed by using:
  • START TIMER
  • ... your code here ...
  • print _elapsed // elapsed time in seconds since the last START TIMER

x = 90 start timer for i=1 to 1_000_000 y = sin(x) next i print 'Elapsed time: '; _elapsed

The debugging and tracing commands, in one place

Every switch below is a statement, so it can go in a program, and a console command, typed at the >>> prompt. debug on is the one the others depend on: it turns on the debugging mode that tracing, statistics and breakpoints need, and it synchronizes terminal output so that what you see is current when the program stops. A program runs slower in that mode, which is why it is a switch and not the default.

run 'myprogram' loads the program and starts it clean — debugging, tracing and statistics all off — so a switch typed before it is forgotten. The console switches are for a program that is already there:

  • Halt it, switch, and go. Stop the running program with alt/b (or let it reach a break), type debug on or debug off — or any switch below — and then go. The program continues from where it stopped, in the new mode.
  • Run the loaded program again. Once the program is loaded, type the switch and then a bare run (no name): it runs the same program again and keeps the mode.
CommandWhat it does
debug on Turn on the debugging mode. Required by everything below.
debug off Turn it off. This also turns tracing and statistics off.
_debug Not a command: a system variable a program can test. It is 1 (true) while the debugging mode is on and 0 (false) when it is off.
trace on Open the Program Trace window and log every statement as it runs: the time, its MAIN.line label, and the source text.
trace routine: on Log only the routines entered, one line each, indented two spaces per level of call — an outline of the program's higher-level logic. trace on switches back to every statement.
trace off / trace routine: off Stop tracing and close the window. Closing the window does the same.
stats on / stats off Start and stop the profiler's routine timers, described above.
list stats Write the profile to myprogram_stats.txt.
break A breakpoint in the source: under debug on the program stops there and drops into the console. In a normal run it does nothing (and the compiler warns, since it is not a loop exit).
step / step 5 At a halt: run the next statement, or the next five, and stop again. Esc is a one-tap step; F1 steps over a routine call, F3 steps out of the routine.
go Resume the program from where it stopped.
alt/b The key, not a command: halt the running program and drop into the console at that statement. go continues.
show all Dump every variable, array, table and cluster, the open files and the call stack to myprogram_debug_time.txt.
debug all / debug all "file.txt" The same dump from inside a program, to the default file or the one named.
debug show x, y$ Dump just the variables listed.
stop End the program.

The two habits that pay off: put debug on and stats on at the top of a program you are studying and list stats before it ends, and for an unfamiliar application, once it is loaded, type trace routine: on at the console and then a bare run — the trace window then reads as an outline of what the application does.

Is debugging on? The _debug system variable

A program can ask whether the debugging mode is on: _debug is 1 (true) after debug on and 0 (false) after debug off, and a program started with run 'myprogram' begins with it 0. It is a true-or-false value, so it goes straight into an if:

report_mode debug on report_mode debug off report_mode end routine report_mode if _debug then print "debugging is ON (_debug = "; _debug; ")" else print "debugging is off (_debug = "; _debug; ")" end if end routine

Output:

debugging is off (_debug = 0 ) debugging is ON (_debug = 1 ) debugging is off (_debug = 0 )

Use it to keep diagnostic work out of a normal run without editing the program: extra print lines, a debug show of the variables you are watching, or a slower consistency check can sit behind if _debug then and cost nothing until debug on is given. At the console, print _debug answers the same question — and after a halt, debug off then go lets the program run on with _debug reading 0.


Summary: The Importance of Profiling in Development

Profiling is a powerful built-in feature of Sheerpower that helps developers optimize performance by identifying routines that consume the most time and resources. By targeting bottlenecks, the profiler allows developers to concentrate their efforts where they will have the greatest impact, significantly improving overall efficiency.

By providing detailed insights into execution times and routine call frequencies, the profiler enables data-driven optimization, simplifies debugging, and minimizes trial-and-error in development. This results in faster, more cost-effective, and high-performing applications tailored to business needs. Profiling turns the often complex process of optimization into a precise and measurable task.


Hide Description

    

       


      

Enter or modify the code below, and then click on RUN

Looking for the full power of Sheerpower?
Check out the Sheerpower website. Free to download. Free to use.