World: r3wp
[Rebol School] Rebol School
older newer | first last |
Steeve 2-Jan-2009 [1078x2] | Brian, how much would you size the preallocating ? |
half of the intial serie length ? | |
BrianH 2-Jan-2009 [1080] | map: func [ {Evaluates a block for each value(s) in a series and returns them as a block.} 'word [word! block!] "Word or block of words to set each time (local)" data [block!] "The series to traverse" body [block!] "Block to evaluate each time" ] [ foreach :word data reduce [:append make block! either word? word [length? data] [divide length? data length? word] to paren! body] ] |
Steeve 2-Jan-2009 [1081] | humm, really accurate |
BrianH 2-Jan-2009 [1082] | That expression is faster than the reallocations for large data. |
Steeve 2-Jan-2009 [1083] | agree |
BrianH 2-Jan-2009 [1084] | map: func [ [throw] {Evaluates a block for each value(s) in a series and returns them as a block.} 'word [word! block!] "Word or block of words to set each time (local)" data [block!] "The series to traverse" body [block!] "Block to evaluate each time" ] [ foreach :word data reduce [ :append make block! either word? word [length? data] [divide length? data length? word] to paren! body ] ] |
[unknown: 5] 2-Jan-2009 [1085] | I would change the append to insert tail just because it is faster. |
Steeve 2-Jan-2009 [1086] | yep, agreed too |
BrianH 2-Jan-2009 [1087] | Yeah, I forgot that append is mezzanine in R2 :( |
Steeve 2-Jan-2009 [1088] | even shorter with a to-block instead of either: foreach :word data reduce [ :insert :tail make block! divide length? data length? to-block word] to paren! body ] |
BrianH 2-Jan-2009 [1089] | to-block has block copy overhead, but yeah. |
Steeve 2-Jan-2009 [1090x2] | it's just for intialisation... ;-) |
*during | |
btiffin 2-Jan-2009 [1092] | /me quietly files into the lecture hall and sits down as a crowd gathers ... |
BrianH 2-Jan-2009 [1093x2] | I'll profile to see which is faster. We already have block creation overhead with the reduce and to-paren. |
Actually, as overhead goes the to-block would be minimal in comparison :) | |
[unknown: 5] 2-Jan-2009 [1095x2] | But I would go with the more efficient code in that situation since it would be a mezz |
We can care less how long the code is in a mezz as long as it performs at the optimum. | |
Steeve 2-Jan-2009 [1097] | Brian, i wonder what will be the overhead if map was a mezzanine in R3 (you should test that too) ? |
BrianH 2-Jan-2009 [1098x2] | Since the profiling functions are better in R3, I am comparing the differences there. |
The either is faster. | |
[unknown: 5] 2-Jan-2009 [1100x2] | pick is usually faster than either when eithers first argument is logic based. |
which in this case it is | |
Steeve 2-Jan-2009 [1102] | ahah, i was thinking about pick too |
BrianH 2-Jan-2009 [1103] | Yes, but we have to calculate. Let me check that too. |
Steeve 2-Jan-2009 [1104] | but in that cas we have to pre-calculate length? data |
[unknown: 5] 2-Jan-2009 [1105x2] | Yes, I agree, alwasy profile - never assume |
at least when building mezzanines. | |
BrianH 2-Jan-2009 [1107] | I still want to backport the profiler to R2. |
Steeve 2-Jan-2009 [1108] | length? pick reduce [[1] data] word? word but there is an overhead too |
BrianH 2-Jan-2009 [1109] | Here's the fastest version for R2: map: func [ [throw] "Evaluates a block for each value(s) in a series and returns them as a block." 'word [word! block!] "Word or block of words to set each time (local)" data [block!] "The series to traverse" body [block!] "Block to evaluate each time" ] [ head foreach :word data reduce [ :insert :tail make block! either word? word [length? data] [divide length? data length? word] to paren! body ] ] |
Janko 2-Jan-2009 [1110] | btw: python was recieving criticism because guido made it more and more awkward to use functional programming , which wasn't handeled that well previoulsly either. The way you added a powerful (with variable number of params) map that looks like native in other languages really speaks something about a language. Interestingly this article about python also mentions REBOL http://lambda-the-ultimate.org/node/2302 |
BrianH 2-Jan-2009 [1111] | Interesting to see how much optimization expands the code, isn't it? |
Janko 2-Jan-2009 [1112] | yes :) |
Steeve 2-Jan-2009 [1113x2] | and [divide a b] is faster than [/ a b] ? |
lol | |
BrianH 2-Jan-2009 [1115x2] | LENGTH? is prefix, so to use the infix / you would need parens, which have overhead. |
I wouldn't use prefix / in a mezzanine - bad form. | |
Steeve 2-Jan-2009 [1117] | in R2 / works as prefix too |
BrianH 2-Jan-2009 [1118] | I can't profile them for comparison, because bad form like that has not removed from R3, where my profiling functions are better. |
Steeve 2-Jan-2009 [1119] | anyway... |
BrianH 2-Jan-2009 [1120] | has not -> has |
Steeve 2-Jan-2009 [1121] | it was a joke |
BrianH 2-Jan-2009 [1122x2] | has not -> has been I am bad at typing today. Let's not corrupt people in the school group :) |
Speaking of which, good catch on the foreach bind/copy eliminating an explicit copy :) | |
Steeve 2-Jan-2009 [1124] | yeah, i think too, i will have to remember that trick next time |
BrianH 2-Jan-2009 [1125x3] | Couldn't use it here efficiently, but a good trick nonetheless :) |
Btw, on R3 that EITHER WORD? trick has half the overhead of the TO-BLOCK method. | |
Not much either way, but we count our miliseconds when writing mezzanines. | |
older newer | first last |