Search code examples
sortingschemeracketfunction-calls

Racket Function Calls Count


Hey all,

So in my never-ending quest to learn more Racket, I'm trying to understand set! calls and how it can be used as a counter for counting function calls.

I coded some basic sorting algorithms (merge, insertion, quick) that are dependent on a comparison operator (i.e: <, >, <=), and I want to know how many times the operator is called to try and figure out its efficiency. The code format I'm working with is:

(count-compares sort compare? lst)

where sort is the sorting method I coded, compare? is the comparison operator, and lst is the list of integers to be sorted. I'm open to changing the format if you think there's a better approach.

I know compare? needs to be broken off (using another function??) to include a set!, but I'm not sure how to do that. Does anyone have any suggestions on where to start?

Thanks! <3


Solution

  • You can do it directly and without any package!

    Define outside off all methods a var: (define count-comp 0)

    to your compare-function add the statement (set! count-comp (+ count-comp 1))

    Later use (display count-comp) (set! count-comp 0)