memory and cprint
- Posted by Arthur Adamson <euclid at ISOC.NET> Apr 17, 1998
- 558 views
Subject: memory.e and cprint.e Bob Craig sent 2 handy routines which I have attached as cprint.e and space.e ...just to make sure you don't miss them. They work like a charm. cprint makes sequences much easier to read when working with text. Memory permits easy comparison of alternate means of representation. Bob, thanks a lot. Art ------------------------------------------------------------ cprint.e include graphics.e global procedure cprint(object x) -- display an object with colored ASCII characters if atom(x) then printf(1, "%g", x) if integer(x) and x >= 32 and x <= 127 then text_color(BRIGHT_MAGENTA) if x = 32 then puts(1, 254) -- block character else puts(1, x) end if text_color(WHITE) end if else -- sequence puts(1, "{") for i = 1 to length(x) do cprint(x[i]) if i < length(x) then puts(1, ',') end if end for puts(1, "}") end if end procedure --cprint(65) --puts(1, '\n') --cprint(999.9) --puts(1, '\n') --cprint({1, 32, 42, 52 ,97, 155.5}) --puts(1, '\n') --cprint("Euphoria") --puts(1, '\n') --You could add a procedure that calls cprint(), --then does puts(1, '\n'). You can't just add it to cprint() itself --because of it's recursive nature. ------------------------------------------------ space.e global function bytes_needed(object x) -- estimates the number of bytes of storage needed for any -- Euphoria 2.0 data object (atom or sequence). integer space if integer(x) then return 4 elsif atom(x) then return 16 else -- sequence space = 24 -- overhead for i = 1 to length(x) do space = space + bytes_needed(x[i]) end for return space end if end function --? bytes_needed(1) -- 4 --? bytes_needed(1.5) -- 16 --? bytes_needed({1,2,3}) -- 36 --? bytes_needed({{1.5,2.5}, {1,2}}) -- 112 --? bytes_needed({{1,2},{}, {},{1,2}}) -- 88 + 24 for each {} --? bytes_needed({{1,2}, 1.234 , {},{1,2}}) -- 88 + 16 for each float --? bytes_needed({{1,2}, 0 , 0 ,{1,2}}) -- 88 + 4 for each zero or integer --Keep in mind that Euphoria will often point to a --sequence or a (non-integer) atom, rather than make --a copy of it. For example: --x = repeat("Euphoria", 100) --y = 99.9 --z = y --There will only be *one* copy of "Euphoria" in memory, --along with 100 4-byte pointers to it. There will only be --one copy of 99.9, pointed to by y and z. --Euphoria is primarily tuned for speed. It does not --for instance, try to store small integers in less than --4 bytes. The space overhead on sequences --consists of the length, plus a bunch of information --that allows for fast manipulation (growing/shrinking) --of sequences, as well as fast allocation and --deallocation of storage. -- --Whenever you have dynamic allocation of storage, --you have to use some storage to keep track of --which blocks of memory are used vs. free, their --length etc. That's included in the above figures. --Regards, --Rob Craig --Rapid Deployment Software Arthur P. Adamson, The Engine Man, euclid at isoc.net