Function frequency
Function frequency is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.
Display - for a program or runtime environment (whatever suites the style of your language) - the top ten most frequently occurring functions.
This is a static analysis: The question is not how often each function is actually executed at runtime, but how often it is used by the programmer.
Go
Only crude approximation is currently easy in Go. The following parses source code, looks for function call syntax (an expression followed by an argument list) and prints the expression. <lang go>package main
import (
"fmt" "go/ast" "go/parser" "go/token" "io/ioutil" "os"
)
func main() {
if len(os.Args) != 2 { fmt.Println("usage ff <go source filename>") return } src, err := ioutil.ReadFile(os.Args[1]) if err != nil { fmt.Println(err) return } fs := token.NewFileSet() a, err := parser.ParseFile(fs, os.Args[1], src, 0) if err != nil { fmt.Println(err) return } f := fs.File(a.Pos()) m := make(map[string]int) ast.Inspect(a, func(n ast.Node) bool { if ce, ok := n.(*ast.CallExpr); ok { start := f.Offset(ce.Pos()) end := f.Offset(ce.Lparen) m[string(src[start:end])]++ } return true }) for k, v := range m { fmt.Println(v, k) }
}</lang> Output, when run on source code above:
4 fmt.Println 1 string 1 a.Pos 1 ast.Inspect 1 ioutil.ReadFile 1 len 2 f.Offset 1 token.NewFileSet 1 ce.Pos 1 make 1 fs.File 1 parser.ParseFile
PicoLisp
<lang PicoLisp>(let Freq NIL
(for "L" (filter pair (extract getd (all))) (for "F" (filter atom (fish '((X) (or (circ? X) (getd X))) "L" ) ) (accu 'Freq "F" 1) ) ) (for X (head 10 (flip (by cdr sort Freq))) (tab (-7 4) (car X) (cdr X)) ) )</lang>
Output:
quote 310 car 236 cdr 181 setq 148 let 136 if 127 and 124 cons 110 cadr 80 or 76