[REBOL] creation of a block with a lot of integers Re:(2)
From: peoyli:algonet:se at: 6-Oct-2000 10:59
This is a MIME encoded multipart message. The fact that you are reading
this means you don't have a MIME capable mail program. You might still
be able to read part of the mail's content, but some of it may require
a MIME capable mail reader to decode. Following are some URLs where
you can find MIME-capable mail programs for common platforms:
Amiga............: MicroDot-II http://www.vapor.com/
Unix.............: Metamail ftp://ftp.bellcore.com/nsb/
Windows/Macintosh: Eudora http://www.qualcomm.com/
General info about MIME can be found at:
http://www.cis.ohio-state.edu/hypertext/faq/usenet/mail/mime-faq/top.html
--=_=8<==MD239DE23B4-42B45C89==8<=_Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
> Hi, peoyli,
>
...
> It would be interesting to have you try a couple of the above options
> along with your original for version and post the timings, if you
> have the spare time.
>
> -jn-
Ok,,
here comes my test results, along with the code being tested..
Method 0: 1st try: unallocated block, for-loop, insert
Method 1: 2nd try: allocated block, for-loop, insert tail
Method 2: 3rd try: allocated block, modified iota to accept a minimum value (repeat-loop)
Method 3: 4th try: allocated block, map iota... (repeat-loop)
Method 4: 5th try: allocated block, filter iota / range...
Method 5: 6th try: allocated block, fromto function... (while-loop)
Summary:
Method 0 is slow (only timed with 5000 values)
Method 2 is twice as fast as method 1 (repeat vs. for)
Method 3 always gave incorrect result or failed otherwise (and was also twice as slow
as method 1)
Method 4 did not work with negative values, and was about as slow as method 3
Method 5 is almost as fast as method 2
Method 2 (3rd try) & Method 5 (6th try) are those who produce the
desired result, and is most efficient.
num of values
range
5000 50000 100000 200000 400000
20001-25000 20001-70000 20001-120000 20001-220000 20001-420000
Method
0 0:00:34 - - - -
1 0:00:01 0:00:10 0:00:19 0:00:38 -
2 0:00 0:00:05 0:00:10 0:00:20 0:00:41
3 *1) 0:00:17 *2) 0:00:37 *2 0:01:16 *2) -
4 0:00:05 0:00:17 0:00:30 0:00:59 -
5 0:00 0:00:05 0:00:11 0:00:24 0:00:46
100001 100001
-50000 - 50000 -150000 - -50000
0 - -
1 0:00:20 0:00:18
2 0:00:10 0:00:10
3 0:00:37 *3) 0:00:38 *3)
4 0:00:13 *4) *5)
5 0:00:11 0:00:11
*1) Script Error (caused by the result printing, block size = 0)
27.Work:Programming/REBOL_core_2.3> rebol -q ../REBOL/intblock.r 20001 25000 3
0:00:01 4th try: allocated block, map iota...
Size of block: 0
** Script Error: Out of range or past end.
** Where: first b
*2) Incorrect result
27.Work:Programming/REBOL_core_2.3> rebol -q ../REBOL/intblock.r 20001 70000 3
0:00:17 4th try: allocated block, map iota...
Size of block: 30000
Block's first value: 20001
Block's last value: 50000
27.Work:Programming/REBOL_core_2.3> rebol -q ../REBOL/intblock.r 20001 120000 3
0:00:37 4th try: allocated block, map iota...
Size of block: 80000
Block's first value: 20001
Block's last value: 100000
27.Work:Programming/REBOL_core_2.3> rebol -q ../REBOL/intblock.r 20001 220000 3
0:01:16 4th try: allocated block, map iota...
Size of block: 180000
Block's first value: 20001
Block's last value: 200000
*3) Incorrect result
intblock -50000 50000 3
intblock -150000 -50000 3
0:00:37 4th try: allocated block, map iota...
Size of block: 100001
Block's first value: 1
Block's last value: 100001
*4) Incorrect result
intblock -50000 50000 4
0:00:13 5th try: allocated block, filter iota / range...
Size of block: 50000
Block's first value: 1
Block's last value: 50000
*5) Script Error (caused by the result printing, block size = 0)
0:00 5th try: allocated block, filter iota / range...
Size of block: 0
** Script Error: Out of range or past end.
** Where: first b
--=_=8<==MD239DE23B4-42B45C89==8<=_Content-Type: text/plain; charset=us-ascii; name="intblock.r"
Content-Transfer-Encoding: plain (7/8 bit)
Content-Disposition: attachment; filename="intblock.r"
X-MD2-FilePath: Work:Programming/REBOL/intblock.r
REBOL []
; Parameters: num, min
iota_mod: function [n [integer!] min [integer!]] [r i] [
r: make block! n
repeat i n [insert tail r i + min - 1]
r
]
; Parameters: num
iota: function [n [integer!]] [r i] [
r: make block! n
repeat i n [insert tail r i]
r
]
map: function [[catch] b [block!] f [any-function!]] [r v] [
r: make block! length? b
foreach c b [
if found? v: do [f c] [insert/only tail r v]
]
r
]
filter: function [
[catch] b [block!] f [any-function!]
][
r v
][
r: make block! length? b
foreach c b [
if do [f c] [insert/only tail r c]
]
r
]
range: func [lb [integer!] ub [integer!]] [
filter iota ub func [n] [n >= lb]
]
fromto: function [lb [integer!] ub [integer!]] [r] [
r: make block! (ub - lb + 1)
while [lb <= ub] [insert tail r lb lb: lb + 1]
r
]
intblock: func [
"Create an array with a range of integers"
min [integer!] "The lowest number to include"
max [integer!] "The highest number to include"
meth [integer!] "Method of creating block with values"
/local temp
][
if min > max [temp: min min: max max: temp]
num: max - min + 1
switch meth [
; 1st try: unallocated block, for-loop, insert
0 [
tim1: now/time
b: [] clear b
for temp min max 1 [ insert b temp ] ; Generate array with valid numbers
print reduce [now/time - tim1 " 1st try: unallocated block, for-loop, insert"]
]
; 2nd try: allocated block, for-loop, insert tail
1 [
tim1: now/time
b: make block! num
for temp min max 1 [ insert tail b temp ] ; Generate array with valid numbers
print [now/time - tim1 " 2nd try: allocated block, for-loop, insert tail"]
]
; 3rd try: allocated block, modified iota to accept a minimum value
2 [
tim1: now/time
b: iota_mod abs (max - min + 1) min
print [now/time - tim1 " 3rd try: allocated block, modified iota to accept a minimum
value"]
]
; 4th try: allocated block, map iota...
3 [
tim1: now/time
b: map iota (max - min + 1) func [n] [if n >= min [n]]
print [now/time - tim1 " 4th try: allocated block, map iota..."]
]
; 5th try: allocated block, filter iota / range...
4 [
tim1: now/time
b: range min max
print [now/time - tim1 " 5th try: allocated block, filter iota / range..."]
]
; 6th try: allocated block, fromto function...
5 [
tim1: now/time
b: fromto min max
print [now/time - tim1 " 6th try: allocated block, fromto function..."]
]
]
print reduce ["Size of block: " length? b]
print reduce ["Block's first value: " first b]
print reduce ["Block's last value: " last b]
b
]
;args: load system/script/args
;args: parse first args " "
;min: to-integer args/1
;max: to-integer args/2
;meth: to-integer args/3
;intblock min max meth
intblock -150000 -50000 4
--=_=8<==MD239DE23B4-42B45C89==8<=_=--
(end of MIME multipart message)