Re: [racket-users] Strange performance behavior

2020-08-09 Thread sleepnova
Oh, I got it. Thank you for the explanation!

George Neuner  於 2020年8月9日 週日 下午1:35寫道:

>
> On 8/9/2020 1:20 AM, wanp...@gmail.com wrote:
> >
> > One more thing which bothers me is if I put a (collect-garbage) in
> > front of the testing, I got gc time: 0 if not I got gc time: 9.
> > Why can't 1 gc reclaim all memory during execution while it can before
> > executes?
>
> Those numbers show *time* spent working, not what was done.  If you
> collect before running your program, at that point little has been
> allocated, and little or nothing has been freed, and so the GC has
> little to do ... hence it spends '0' time doing it  [zero meaning below
> the resolution of the computer's clock].  Once your program starts
> running, memory is being allocated and freed, and so a GC in the middle
> or at the end has much more work to do.
>
> George
>
>
>

-- 
- sleepnova
呼叫小黃創辦人 & CEO

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/CABa2-7PZ_Yhr7_5nqTjBA09hExyWOdM0gyQAvuOhGFb8XwbivA%40mail.gmail.com.


Re: [racket-users] Strange performance behavior

2020-08-08 Thread George Neuner



On 8/9/2020 1:20 AM, wanp...@gmail.com wrote:


One more thing which bothers me is if I put a (collect-garbage) in 
front of the testing, I got gc time: 0 if not I got gc time: 9.
Why can't 1 gc reclaim all memory during execution while it can before 
executes?


Those numbers show *time* spent working, not what was done.  If you 
collect before running your program, at that point little has been 
allocated, and little or nothing has been freed, and so the GC has 
little to do ... hence it spends '0' time doing it  [zero meaning below 
the resolution of the computer's clock].  Once your program starts 
running, memory is being allocated and freed, and so a GC in the middle 
or at the end has much more work to do.


George


--
You received this message because you are subscribed to the Google Groups "Racket 
Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/8499ad0d-a5a7-424b-1027-df25b255bc61%40comcast.net.


Re: [racket-users] Strange performance behavior

2020-08-08 Thread wanp...@gmail.com
Thanks, that make sense!

One more thing which bothers me is if I put a (collect-garbage) in front of 
the testing, I got gc time: 0 if not I got gc time: 9.
Why can't 1 gc reclaim all memory during execution while it can before 
executes?

Sam Tobin-Hochstadt 在 2020年8月5日 星期三下午11:44:21 [UTC+8] 的信中寫道:

> What's happening here is that your function takes effectively 0 time, 
> but when you ran the first version, there was a GC pause during it 
> (that's why there's the "gc time: 9" there). GC pauses can happen at 
> any time, basically, so it's not something about what your function is 
> doing. 
>
> Here's a benchmark of your two functions that takes long enough to run 
> that it avoids some of these issues, and also runs a GC before 
> benchmarking: https://gist.github.com/7cb4645308d8572e2250833ef7b90b7c 
>
> On my machine, I get 40 ms for version 1, and 100 ms for version 2, as 
> you expected. 
>
> Sam 
>
> On Wed, Aug 5, 2020 at 11:21 AM wanp...@gmail.com  
> wrote: 
> > 
> > I was working on a exercism problem named Raindrops. 
> > 
> > Problem description: 
> > Convert a number to a string, the contents of which depend on the 
> number's factors. 
> > 
> > If the number has 3 as a factor, output 'Pling'. 
> > If the number has 5 as a factor, output 'Plang'. 
> > If the number has 7 as a factor, output 'Plong'. 
> > If the number does not have 3, 5, or 7 as a factor, just pass the 
> number's digits straight through. 
> > 
> > I came out with two version. 
> > 
> > ; version 1 
> > (define (convert n) 
> > (define pling (divides? 3 n)) 
> > (define plang (divides? 5 n)) 
> > (define plong (divides? 7 n)) 
> > (if (or pling plang plong) 
> > (string-append (if pling "Pling" "") 
> > (if plang "Plang" "") 
> > (if plong "Plong" "")) 
> > (number->string n))) 
> > 
> > ; version 2 
> > (define (convert n) 
> > (define table '((3 . "Pling") (5 . "Plang") (7 . "Plong"))) 
> > (define result (for/list ([(k v) (in-dict table)] #:when (divides? k n)) 
> v)) 
> > (if (empty? result) (number->string n) 
> > (string-append* result))) 
> > 
> > (require math/number-theory) 
> > 
> > I thought version 1 would be faster, but it turned out to be wrong. 
> Running with raco test got following timing information. 
> > 
> > version 1 
> > cpu time: 9 real time: 9 gc time: 9 
> > version 2 
> > cpu time: 0 real time: 0 gc time: 0 
> > 
> > Then I ran both version in DrRacket, both output following result. 
> > cpu time: 0 real time: 0 gc time: 0 
> > 
> > It's strange, isn't it? 
> > 
> > -- 
> > You received this message because you are subscribed to the Google 
> Groups "Racket Users" group. 
> > To unsubscribe from this group and stop receiving emails from it, send 
> an email to racket-users...@googlegroups.com. 
> > To view this discussion on the web visit 
> https://groups.google.com/d/msgid/racket-users/46592171-c357-4897-af1a-bea91c838cacn%40googlegroups.com.
>  
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/e7d0d239-1b36-4e7d-a93d-7dbdfcbcc04en%40googlegroups.com.


Re: [racket-users] Strange performance behavior

2020-08-07 Thread 'Joel Dueck' via Racket Users
On Wednesday, August 5, 2020 at 10:44:21 AM UTC-5 Sam Tobin-Hochstadt wrote:

> Here's a benchmark of your two functions that takes long enough to run 
> that it avoids some of these issues, and also runs a GC before 
> benchmarking: https://gist.github.com/7cb4645308d8572e2250833ef7b90b7c 
>

What is the reason for calling `collect-garbage` twice consecutively? 

Also, the docs are unclear on this, but is `(collect-garbage)` equivalent 
to `(collect-garbage 'major)` ?

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/6dbc0daf-7236-471e-9a4f-35164a33ca6fn%40googlegroups.com.


Re: [racket-users] Strange performance behavior

2020-08-05 Thread Sam Tobin-Hochstadt
What's happening here is that your function takes effectively 0 time,
but when you ran the first version, there was a GC pause during it
(that's why there's the "gc time: 9" there). GC pauses can happen at
any time, basically, so it's not something about what your function is
doing.

Here's a benchmark of your two functions that takes long enough to run
that it avoids some of these issues, and also runs a GC before
benchmarking: https://gist.github.com/7cb4645308d8572e2250833ef7b90b7c

On my machine, I get 40 ms for version 1, and 100 ms for version 2, as
you expected.

Sam

On Wed, Aug 5, 2020 at 11:21 AM wanp...@gmail.com  wrote:
>
> I was working on a exercism problem named Raindrops.
>
> Problem description:
> Convert a number to a string, the contents of which depend on the number's 
> factors.
>
> If the number has 3 as a factor, output 'Pling'.
> If the number has 5 as a factor, output 'Plang'.
> If the number has 7 as a factor, output 'Plong'.
> If the number does not have 3, 5, or 7 as a factor, just pass the number's 
> digits straight through.
>
> I came out with two version.
>
> ; version 1
> (define (convert n)
>   (define pling (divides? 3 n))
>   (define plang (divides? 5 n))
>   (define plong (divides? 7 n))
>   (if (or pling plang plong)
>   (string-append (if pling "Pling" "")
>  (if plang "Plang" "")
>  (if plong "Plong" ""))
>   (number->string n)))
>
> ; version 2
> (define (convert n)
>   (define table '((3 . "Pling") (5 . "Plang") (7 . "Plong")))
>   (define result (for/list ([(k v) (in-dict table)] #:when (divides? k n)) v))
>   (if (empty? result) (number->string n)
>   (string-append* result)))
>
> (require math/number-theory)
>
> I thought version 1 would be faster, but it turned out to be wrong. Running 
> with raco test got following timing information.
>
> version 1
> cpu time: 9 real time: 9 gc time: 9
> version 2
> cpu time: 0 real time: 0 gc time: 0
>
> Then I ran both version in DrRacket, both output following result.
> cpu time: 0 real time: 0 gc time: 0
>
> It's strange, isn't it?
>
> --
> You received this message because you are subscribed to the Google Groups 
> "Racket Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to racket-users+unsubscr...@googlegroups.com.
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/racket-users/46592171-c357-4897-af1a-bea91c838cacn%40googlegroups.com.

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/CAK%3DHD%2BYN9USK2qU4WsoNhkeZtFn%2B5DfS4uNaZR0t%3DvjfZQ%2ByqQ%40mail.gmail.com.


Re: [racket-users] Strange performance behavior

2020-08-05 Thread sleepnova
Sorry, forgot to attach the test suite.

wanp...@gmail.com  於 2020年8月5日 週三 下午11:21寫道:

> I was working on a exercism problem named Raindrops.
>
> Problem description:
> Convert a number to a string, the contents of which depend on the number's
> factors.
>
> If the number has 3 as a factor, output 'Pling'.
> If the number has 5 as a factor, output 'Plang'.
> If the number has 7 as a factor, output 'Plong'.
> If the number does not have 3, 5, or 7 as a factor, just pass the number's
> digits straight through.
>
> I came out with two version.
>
> ; version 1
> (define (convert n)
>   (define pling (divides? 3 n))
>   (define plang (divides? 5 n))
>   (define plong (divides? 7 n))
>   (if (or pling plang plong)
>   (string-append (if pling "Pling" "")
>  (if plang "Plang" "")
>  (if plong "Plong" ""))
>   (number->string n)))
>
> ; version 2
> (define (convert n)
>   (define table '((3 . "Pling") (5 . "Plang") (7 . "Plong")))
>   (define result (for/list ([(k v) (in-dict table)] #:when (divides? k n))
> v))
>   (if (empty? result) (number->string n)
>   (string-append* result)))
>
> (require math/number-theory)
>
> I thought version 1 would be faster, but it turned out to be wrong.
> Running with raco test got following timing information.
>
> version 1
> cpu time: 9 real time: 9 gc time: 9
> version 2
> cpu time: 0 real time: 0 gc time: 0
>
> Then I ran both version in DrRacket, both output following result.
> cpu time: 0 real time: 0 gc time: 0
>
> It's strange, isn't it?
>
> --
> You received this message because you are subscribed to a topic in the
> Google Groups "Racket Users" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/racket-users/LhCM51vnywg/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to
> racket-users+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/racket-users/46592171-c357-4897-af1a-bea91c838cacn%40googlegroups.com
> 
> .
>


-- 
- sleepnova
呼叫小黃創辦人 & CEO

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/CABa2-7MsirGY2p7ib%3D7a6Nb%3D49%2BW%3DK3uReJvepc_fqs0NbSmQw%40mail.gmail.com.


raindrops-test.rkt
Description: Binary data


[racket-users] Strange performance behavior

2020-08-05 Thread wanp...@gmail.com
I was working on a exercism problem named Raindrops.

Problem description:
Convert a number to a string, the contents of which depend on the number's 
factors.

If the number has 3 as a factor, output 'Pling'.
If the number has 5 as a factor, output 'Plang'.
If the number has 7 as a factor, output 'Plong'.
If the number does not have 3, 5, or 7 as a factor, just pass the number's 
digits straight through.

I came out with two version.

; version 1
(define (convert n)
  (define pling (divides? 3 n))
  (define plang (divides? 5 n))
  (define plong (divides? 7 n))
  (if (or pling plang plong)
  (string-append (if pling "Pling" "")
 (if plang "Plang" "")
 (if plong "Plong" ""))
  (number->string n)))

; version 2
(define (convert n)
  (define table '((3 . "Pling") (5 . "Plang") (7 . "Plong")))
  (define result (for/list ([(k v) (in-dict table)] #:when (divides? k n)) 
v))
  (if (empty? result) (number->string n)
  (string-append* result)))

(require math/number-theory)

I thought version 1 would be faster, but it turned out to be wrong. Running 
with raco test got following timing information.

version 1
cpu time: 9 real time: 9 gc time: 9
version 2
cpu time: 0 real time: 0 gc time: 0

Then I ran both version in DrRacket, both output following result.
cpu time: 0 real time: 0 gc time: 0

It's strange, isn't it?

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/46592171-c357-4897-af1a-bea91c838cacn%40googlegroups.com.