Notifications
Clear all

[Closed] Bistream Convertion

(
	fn bitConvert bistream: fSize: =
	(
		str=""
		for i = 1 to fSize do str += bit.intAsChar (readbyte bistream)
		str
	)


	
	myFile=@"D:	est.txt"
	sStream = fopen myFile "rb"
	gc()
	local st = timeStamp()
	local mem = heapFree
	
	print (bitConvert bistream:sStream fSize:(getfilesize myFile))
	fclose sStream
	format "Time: % ms, memory: %
" (timestamp()-st) (mem-heapfree)	
)	

Here I provide my example which works well but takes long, if you get a file with more then 100 kb then you will notice it.
Please help me do it faster, please!!!

7 Replies

For a 70Kb file (very small), the following modifications should make it about 70 times faster and use no memory at all instead of the 2.5GB used by yhe original version.

(
  fn bitConvert bistream: fSize: =
  (
  	intAsChar = bit.intAsChar
  	stream = stringstream ""
  	for i = 1 to fSize do format "%" (intAsChar (readbyte bistream)) to:stream
  	return (stream as string)
  )
  )

– time:5638 ram:13858480L (memory leak ouside the Max Heap. 2.5GB)
– time:79 ram:9243440L

1 Reply
(@denist)
Joined: 1 year ago

Posts: 0

this is an example of the perfect use of the mxs… maybe the ‘return’ is just not necessary
and bistream has an end. finally ‘readbyte’ returns undefined

It makes me feel better when I see it.

I can’t reproduce it.

If readByte() would return "undefined" shouldn't I get an exception from bit.intAsChar() ?
-- Unable to convert: undefined to type: Integer

well. i’ve promised myself to not touch the max for a while.

just check ‘if the reading byte result is not undefined then…’

Wow, thank youuuu, Denis and Jorge!
If this undefined should be at the end, shouldn’t we use #eof or may be for i = 1 to fSize-1 ?

I tested now with a file 130Kb this line

for i = 1 to fSize do if (readbyte bistream)==undefined then print “I’m undefined”

It doesnt print undefined, but if I put

for i = 1 to fSize+1 do if (readbyte bistream)==undefined

then I get the result “I’m undefined”

Or we can use instead of for i = 1 to fSize

while (val = ReadByte bistream) != undefined do format “%” (intAsChar val) to:stream

But it seams to me that while uses more memory then for