• Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

World: r4wp

[#Red] Red language group

Arnold
15-Jun-2013
[8365x2]
No need to defend me now. I hope I am wrong, it happens ;) Should 
have made the folder public do that anyway.
randompublic folder added.
Pekr
15-Jun-2013
[8367]
Still prompt to jump to conclusions without even looking at what 
the code does
 - can you really read my message as jumping to any conclusion?
DocKimbel
15-Jun-2013
[8368x3]
Yes, you were comparing the speed of two programs without being sure 
they were implementing exactly the same algorithm.
Knuth's code could compete at C obfuscated contests. Arnold seems 
to have done a literal translation to Red/System but keeping the 
same obfuscated symbols, so not easy to read. However, it seems at 
first look that the algorithm has been well preserved.
Congrats to Arnold for doing this conversion and getting the right 
results! :)
Kaj
15-Jun-2013
[8371x4]
Here's a bug:
ran_arr_ptr/value: ran_arr_dummy   ;; the next random number, or 
-1
should be
ran_arr_ptr/value: :ran_arr_dummy
Pekr
15-Jun-2013
[8375]
Yes, you were comparing

 - wrong - I was not comparing anything, nor complaining to anything 
 ;-) My question was more general, headed towards if in regards to 
 red/system architecture, the measure of being 8x slower than C (in 
 a concrete example guys were talking about), is good, or bad. I simply 
 don't remember outcome of prior discussions, that's all.
Kaj
15-Jun-2013
[8376x15]
In limited tests so far, the indication was that Red/System was roughly 
as fast as unoptimised C, half as fast as optimised C compiled with 
GCC
This is a more elaborate test, though
Here's a real algorithmic bug:
if  (is_odd(ss) = 1) [
It's deceiving to write this in a form that looks like C. The Red 
evaluation rules compute this as
if is_odd (ss = 1) [
You would have to write
if 1 = is_odd ss [
But it's better to write
#define is_odd(x) [(x) and (1)] ; test on the unit bit of x
as
#define odd? (x) [(as-logic x and 1)]
if odd? ss [
Better keep the parentheses if you're going to use larger expressions 
as parameter:
#define odd? (x) [(as-logic (x) and 1)]
DocKimbel
15-Jun-2013
[8391x2]
Arnold, if you clean up the code and make it look more like Red/System 
and less like C, we could add it to the library folder in the Red 
repo/
My plan for random number support was rather to implement the "Mersenne 
twister" algorithm than the Knuth's one. But in the meantime, Knuth's 
method would fine.

http://en.wikipedia.org/wiki/Mersenne_twister
Kaj
15-Jun-2013
[8393x12]
Yes, we have the Twister in Syllable, too
You have
if  MM < ss [
but the C code has
if (ss>=MM)
so if you really want to reverse it, it has to be
if  MM <= ss [
However, it's more logical to put constants last, and it can also 
generate more optimal code in Red/System
There are more off-by-one errors in there, so the algorithm is not 
the same
The pointer initialisation earlier needs to be
ran_arr_ptr: :ran_arr_dummy
It should actually crash on what you have now, but if it doesn't, 
it pokes a value over an unitialised pointer, so any value in your 
program could be corrupted
Arnold
16-Jun-2013
[8405]
Thanks for the compliment Doc, not really sure what you mean exactly 
by making it more like Red/System and less C: use more descriptive 
names? I will take a closer look at some ed/System examples out there.

Thanks Kaj for finding those and for the tips, the size of MM makes 
it the same in effect in this case, but it has to be <= then. Program 
not crashing, I was lucky then! off-by-one errors? My index goes 
from 1 up, where in C it is from 0 up, I had to debug this to make 
sure elements were swapped in the same way as in the original program. 
That is also why I declare KKP and LLP to as to save from adding 
1 to KK and LL over and over again. 


Knuth's algorythm was the first one I found, and I knew already of 
its existence, so it made sense to use what you have. Sure my Red/System 
code is not optimised.


Going to work on it now and tomorrow, and later I take on the Twister. 
It is a good exercise!
DocKimbel
16-Jun-2013
[8406x2]
Actually, your work on porting that algorithm has many values, like 
showing us the trade-offs of 1-base vs 0-base indexing at Red/System 
level.
About "making it more like Red/System" means applying Red/System 
coding style, which is not formaly defined, but it is very close 
to the official Rebol one. For example, the naming rules for variable 
and function names are the same as in Rebol.
Arnold
16-Jun-2013
[8408x2]
About odd, this solution managed to get all odd number from the even 
ones. Your solution is way more elegant, better fitting the language.

Base-1 Base-0, my personal view on this, it is the programmers choice 
on the level of the source code. What happens beneath the surface 
is compiler/linker sh*t. As a programmer and a human being I start 
to count at 1. 0 is not the new number 1 nor is 1 the new number 
2 etc. It is only an addressing issue, compare to the post. Houses 
in the street are numbered from 1 up to N. The first address a computer 
has in an array is the all 0 address, which is the first "pidgeon-hole" 
to be used. The computer doesn't know 0 as we understand it. Well 
you know all about it.
And the computer must adapt to the human, or the human pulls the 
plug! ;)
DocKimbel
16-Jun-2013
[8410x2]
CPU are optimized for 0-based accesses. Using 1-base indexing will 
make Red/System a bit slower than it needs to be.
OTOH, you can always use pointer arithmetics to get a 0-base indexing 
model.
PeterWood
16-Jun-2013
[8412]
Arnold: does the C code use register variables?
Arnold
16-Jun-2013
[8413]
Yes it does
PeterWood
16-Jun-2013
[8414]
I was going to aslk whether register varaibles still improve performance 
with modern CPUs with on-chip caches?