Skip to content

Commit

Permalink
Added (ms) to record the time in ms.
Browse files Browse the repository at this point in the history
This is useful for benchmarking, so was added to the factorial
test in our test.lisp.
  • Loading branch information
skx committed Sep 25, 2022
1 parent 2e6bbe8 commit d4eb206
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ We have a reasonable number of functions implemented, either in our golang core

Building upon those primitives we have a larger standard-library of functions written in Lisp such as:

* `abs`, `apply`, `append`, `cond`, `count`, `filter`, `lower`, `map`, `min`, `max`, `nat`, `neg`, `now`, `nth`, `reduce`, `repeat`, `reverse`, `seq`, `strlen`, `upper`, `when`, `while`, etc.
* `abs`, `apply`, `append`, `cond`, `count`, `filter`, `lower`, `map`, `min`, `max`, `ms`, `nat`, `neg`, `now`, `nth`, `reduce`, `repeat`, `reverse`, `seq`, `strlen`, `upper`, `when`, `while`, etc.

Although the lists above should be up to date you can check the definitions to see what is currently available:

Expand Down
7 changes: 6 additions & 1 deletion builtins/builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ func PopulateEnvironment(env *env.Environment) {
env.Set("error", &primitive.Procedure{F: errorFn})
env.Set("getenv", &primitive.Procedure{F: getenvFn})
env.Set("now", &primitive.Procedure{F: nowFn})
env.Set("ms", &primitive.Procedure{F: msFn})
env.Set("print", &primitive.Procedure{F: printFn})
env.Set("sort", &primitive.Procedure{F: sortFn})
env.Set("sprintf", &primitive.Procedure{F: sprintfFn})
Expand Down Expand Up @@ -693,10 +694,14 @@ func getenvFn(args []primitive.Primitive) primitive.Primitive {

// nowFn is the implementation of `(now)`
func nowFn(args []primitive.Primitive) primitive.Primitive {

return primitive.Number(time.Now().Unix())
}

// msFn is the implementation of `(ms)`
func msFn(args []primitive.Primitive) primitive.Primitive {
return primitive.Number(time.Now().UnixNano() / int64(time.Millisecond))
}

// matchFn is the implementation of (match ..)
func matchFn(args []primitive.Primitive) primitive.Primitive {

Expand Down
20 changes: 20 additions & 0 deletions builtins/builtins_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1349,6 +1349,26 @@ func TestNow(t *testing.T) {

}

func TestMs(t *testing.T) {

// No arguments
out := msFn([]primitive.Primitive{})

// Will lead to a number
e, ok := out.(primitive.Number)
if !ok {
t.Fatalf("expected number, got %v", out)
}

// Get the current time
tm := int(time.Now().UnixNano() / int64(time.Millisecond))

if math.Abs(float64(tm-int(e))) > 10 {
t.Fatalf("weird result; (ms) != ms - outside our bound of ten seconds inaccuracy")
}

}

func TestGet(t *testing.T) {

// no arguments
Expand Down
5 changes: 3 additions & 2 deletions test.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,9 @@
;; `now` function which times how long it took.
(apply (list 1 10 100 1000 10000 100000)
(lambda (x)
(let ((start (now)))
(print "%s! => %s [%s seconds]" x (fact x) (- (now) start)))))
(let ( (start (now))
(m (ms)) )
(print "%s! => %s [%s seconds / %s ms]" x (fact x) (- (now) start) (- (ms) m) ))))


; Split a string into a list, reverse it, and join it
Expand Down

0 comments on commit d4eb206

Please sign in to comment.