Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Message from discussion Subsets of a list
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Eli Barzilay  
View profile  
 More options Jan 11 2002, 8:10 pm
Newsgroups: comp.lang.scheme
From: Eli Barzilay <e...@barzilay.org>
Date: 11 Jan 2002 20:10:56 -0500
Local: Fri, Jan 11 2002 8:10 pm
Subject: Re: Subsets of a list

George Caswell <tetsu...@maine.rr.com> writes:
> > (define a (subs2 ys 10))
> ;Evaluation took 1760 mSec (0 in gc) 2469846 cells work, 1236201 env, 534
> bytes other
> #<unspecified>
> > (define a (combos ys 10))
> ;Evaluation took 2740 mSec (30 in gc) 675120 cells work, 4721385 env, 34 bytes
> other
> #<unspecified>
> > (define a (subs ys 10))
> ;Evaluation took 1880 mSec (10 in gc) 2663988 cells work, 1242383 env, 34
> bytes other

There is a really nice lesson here -- you have this fight over whose
code runs faster, tweaking your code into something more and more
complex, while Oleg wrote a version which is clearly superior in its
clarity (as he has shown it to be directly following the definition).

So, you must have tried his code too, getting to a conclusion that it
is useless if you care about speed...  I tried it too (s1=subsets,
s2=subs, s3=subs2, s4=combos, s5=subset):

| > (define a '(a b c d e f g h i j k l m n o p q r s t u v w x y z))
| > (time (begin (s1 a 8) #f))
| cpu time: 25520 real time: 25485 gc time: 7580
| > (time (begin (s2 a 8) #f))
| cpu time: 10730 real time: 10715 gc time: 5640
| > (time (begin (s3 a 8) #f))
| cpu time: 7290 real time: 7245 gc time: 2610
| > (time (begin (s4 a 8) #f))
| cpu time: 13720 real time: 13710 gc time: 500
| > (time (begin (s5 a 8) #f))
| cpu time: 66130 real time: 66334 gc time: 41470

and it definitely looks like his solution beats the hell out of the
rest in a worst running time competition...  (And again, wins at
clarity -- the lines in each solution (all re-indented in the same
style) are s1=9, s2=14, s3=18, s4=12, and s5=8 lines.)

The thing is that once you realize that the solution is functional,
you can simply memoize it, and get:

| > (time (begin (s6 a 8) #f))
| cpu time: 5440 real time: 5454 gc time: 1700

clearly beating the rest...

[I always wonder why does everyone teach dynamic programming with
stupid matrices where memoization lets you keep your program exactly
the same and getting the same benefits...]

--
          ((lambda (x) (x x)) (lambda (x) (x x)))          Eli Barzilay:
                  http://www.barzilay.org/                 Maze is Life!


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.