[racket] Optimization

From: Roman Klochkov (kalimehtar at mail.ru)
Date: Mon Jun 16 06:14:55 EDT 2014

 I remade program to avoid optimizing out calulations and reduce GCing

#lang racket
(define (test1 x y z)
(if x
(+ y z)
(- y z)))
(define (test2 x)
(if x
(λ (y z) (+ y z))
(λ (y z) (- y z))))
(define (run1 data)
(foldl (λ (x y) (test1 #t x y)) 0 data))
(define (run2 data)
(foldl (λ (x y) ((test2 #t) x y)) 0 data))
(define data (for/list ([x 140000]) x))
(define acc 0)
(time (set! acc (+ acc (run1 data))))
(time (set! acc (+ acc (run1 data))))
(time (set! acc (+ acc (run1 data))))
(time (set! acc (+ acc (run2 data))))
(time (set! acc (+ acc (run2 data))))
(time (set! acc (+ acc (run2 data))))
(time (set! acc (+ acc (run1 data))))
(time (set! acc (+ acc (run1 data))))
(time (set! acc (+ acc (run1 data)))) Now I have
cpu time: 15 real time: 16 gc time: 0
cpu time: 79 real time: 78 gc time: 63
cpu time: 15 real time: 16 gc time: 0
cpu time: 16 real time: 15 gc time: 0
cpu time: 0 real time: 0 gc time: 0
cpu time: 0 real time: 0 gc time: 0
cpu time: 16 real time: 16 gc time: 0
cpu time: 16 real time: 15 gc time: 0
cpu time: 15 real time: 16 gc time: 0 or sometimes
cpu time: 15 real time: 15 gc time: 0
cpu time: 78 real time: 78 gc time: 63
cpu time: 16 real time: 16 gc time: 0
cpu time: 16 real time: 16 gc time: 0
cpu time: 15 real time: 15 gc time: 0
cpu time: 16 real time: 16 gc time: 0
cpu time: 0 real time: 0 gc time: 0
cpu time: 0 real time: 0 gc time: 0
cpu time: 16 real time: 16 gc time: 0

and even
cpu time: 0 real time: 0 gc time: 0
cpu time: 63 real time: 63 gc time: 47
cpu time: 15 real time: 15 gc time: 0
cpu time: 16 real time: 16 gc time: 0
cpu time: 16 real time: 16 gc time: 0
cpu time: 0 real time: 0 gc time: 0
cpu time: 15 real time: 15 gc time: 0
cpu time: 16 real time: 16 gc time: 0
cpu time: 15 real time: 15 gc time: 0


Where are these zeros from (especially on first run)? If I increse data to 200000, then zeroes disappeared (or become too seldom).

Tested in DrRacket 6.0.1 winXP x86


Mon, 16 Jun 2014 13:52:02 +0400 от Roman Klochkov <kalimehtar at mail.ru>:
>I have "debugging & profiling" on.
>
>Disable debugging. Now have all zeroes :-)
> cpu time: 0 real time: 0 gc time: 0
>
>I will check with optimization. If find something strange, I'll write.
>
>
>Mon, 16 Jun 2014 08:58:16 +0100 от Matthew Flatt <mflatt at cs.utah.edu>:
>>Hm... I'm not able to get anything like the results you're reporting.
>>When I run the enclosed program via `racket` on the command line, I get
>>
>> cpu time: 3 real time: 3 gc time: 0
>> cpu time: 3 real time: 3 gc time: 0
>> cpu time: 2 real time: 2 gc time: 0
>> cpu time: 3 real time: 3 gc time: 0
>> cpu time: 5 real time: 5 gc time: 3
>> cpu time: 2 real time: 2 gc time: 0
>> cpu time: 2 real time: 2 gc time: 0
>> cpu time: 2 real time: 2 gc time: 0
>> cpu time: 4 real time: 4 gc time: 1
>>
>>Running in DrRacket gives messier results, such as
>>
>> cpu time: 17 real time: 10 gc time: 0
>> cpu time: 19 real time: 22 gc time: 0
>> cpu time: 7 real time: 7 gc time: 0
>> cpu time: 15 real time: 8 gc time: 0
>> cpu time: 14 real time: 10 gc time: 0
>> cpu time: 10 real time: 10 gc time: 0
>> cpu time: 7 real time: 6 gc time: 0
>> cpu time: 7 real time: 7 gc time: 0
>> cpu time: 23 real time: 29 gc time: 15
>>
>>or
>>
>> cpu time: 6 real time: 6 gc time: 0
>> cpu time: 5 real time: 4 gc time: 0
>> cpu time: 58 real time: 57 gc time: 49
>> cpu time: 6 real time: 5 gc time: 0
>> cpu time: 5 real time: 4 gc time: 0
>> cpu time: 6 real time: 6 gc time: 0
>> cpu time: 5 real time: 5 gc time: 0
>> cpu time: 5 real time: 4 gc time: 0
>> cpu time: 15 real time: 16 gc time: 9
>>
>>I'm using v6.0.1.12 on Mac OS X 64-bit on a MacBook Pro. Version 6.0.1
>>on the same machine seems to produce the same sorts of results.
>>
>>Any idea what might be different?
>>
>>At Mon, 16 Jun 2014 11:44:41 +0400, Roman Klochkov wrote:
>>>  (define data (for/list ([x 100000]) x))
>>> 
>>> (time (begin0 (void) (run2 data)))
>>> (time (begin0 (void) (run1 data)))
>>> 
>>> 3 times run1, then 3 times run2, then again 3 times run1.
>>> 
>>> Results are stable.
>>> 
>>> Mon, 16 Jun 2014 08:28:10 +0100 от Matthew Flatt < mflatt at cs.utah.edu >:
>>> >I'd expect them to run nearly the same due to inlining and constant
>>> >propagation. If I save your program to "ex.rkt" and use
>>> >
>>> > raco make ex.rkt
>>> > raco decompile ex.rkt
>>> >
>>> >the the output looks almost the same for both functions.
>>> >
>>> >There's a lot of allocation in these programs, of course, and that's
>>> >going to make benchmarking relatively tricky. How are you timing the
>>> >functions, and does it matter whether you `run1` or `run2` first?
>>> >
>>> >At Mon, 16 Jun 2014 11:16:25 +0400, Roman Klochkov wrote:
>>> >>  Strange.
>>> >> 
>>> >> #lang racket
>>> >> (define (test1 x y)
>>> >>   (if x
>>> >>     (+ y 1)
>>> >>     (- y 1)))
>>> >> (define (test2 x)
>>> >>   (if x
>>> >>     (λ (y) (+ y 1))
>>> >>     (λ (y) (- y 1))))
>>> >> (define (run1 data)
>>> >>   (map (λ (x) (test1 #t x)) data))
>>> >> (define (run2 data)
>>> >>   (map (λ (x) ((test2 #t) x)) data)) I expect, that run2 should be faster, 
>>> >> because (test2 #t) returns const (lambda (y) (+ y 1)) and shouldn't be 
>>> checked 
>>> >> on every iteration.
>>> >> 
>>> >> But in reality (time ...) gives 219 for run1 and 212 for run2. run2 is 1.5 
>>> >> times slower!
>>> >> 
>>> >> Why so?
>>> >> 
>>> >> 
>>> >> -- 
>>> >> Roman Klochkov____________________
>>> >>   Racket Users list:
>>> >>  http://lists.racket-lang.org/users
>>> 
>>> 
>>> -- 
>>> Roman Klochkov
>>> ____________________
>>>   Racket Users list:
>>>  http://lists.racket-lang.org/users
>
>
>-- 
>Roman Klochkov


-- 
Roman Klochkov
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20140616/cc545c84/attachment.html>

Posted on the users mailing list.