Let's eat CPU, memory, swap

Started by pjot, May 07, 2005, 06:37:50 AM

Previous topic - Next topic

pjot

Hi all,



By coincidence I found this interesting line:



(array 1 (address "newlisp"))


Don't try this at home kids.



Anyway you PC gets eaten. What happens?



Peter

Lutz

#1
It creates an array with about 4 million cells. On my PC (address "newlisp") returns 4054840 which is the address of the string "newlisp" in memory, taken by the 'array' statement as a number. In my case it took about 2 seconds to create the array but then spends a lot of time displaying 4 miliion 'nil' and the Tcl/Tk text box or your command shell may eventually freeze on Windows (mostly be fine on Linux).



'address' is used when manipulating memory directly. It always returns the address of an item in memory, i.e. the address of a string or a number or float:



(set 'str "newLISP")
(set 'int-num 12345)
(set 'float-num 1.234)

(get-string (address str)) => "newLISP"
(get-int (address int-num)) = 12345
(get-float (address float-num)) = "1.234


You use all of the above frequently when writing interfaces to 'C' libraries. There is also a 'cpymem' which copies memory between two addresses.



As you say: "don't try this at home kids", it is only for grown up newLISP users and people not scared of segfaults, bus errors etc.,  ;)



Lutz

pjot

#2
Well, for me it is perfectly OK that newLisp behaves like this! I made a mistake by trying to initialize an array with a couple of strings, as follows:



(array 3 "this" "is" "wrong")



But according to the docs, only lists can be used to initialize an array:



(array 3 '("this" "is" "right"))



So if I want to initialize an array with strings, it should go like this:



(set 't (array 3))

(nth-set 0 t "this")

(nth-set 1 t "is")

(nth-set 2 t "right")





Or can this be done faster?

Sammo

#3
how about ...



> (define (array-init) (array (length (args)) (args)))

(lambda () (array (length (args)) (args)))



> (set 'a (array-init "one" "two" "three"))

("one" "two" "three")



> a

("one" "two" "three")



> (array? a)

true



or perhaps



> (define (array-init-with-dim dim) (array dim (args)))

(lambda (dim) (array dim (args)))



> (set 'b (array-init-with-dim 5 "one" "two" "three"))

("one" "two" "three" "one" "two")

pjot

#4
OK so an immediate initialization for arrays needs it's own function.... no newLisp internal to accomplish it.



Thanks Sammo!