Creating a large Data of a specified length

LiveCode Builder is a language for extending LiveCode's capabilities, creating new object types as Widgets, and libraries that access lower-level APIs in OSes, applications, and DLLs.

Moderators: LCMark, LCfraser

Post Reply
DarScott
Posts: 227
Joined: Fri Jul 28, 2006 12:23 am
Location: Albuquerque
Contact:

Creating a large Data of a specified length

Post by DarScott » Sat Jul 20, 2019 3:26 pm

I have created a function in LCB that builds a Data filled with zeros based on a specified length. It works fine. (It uses put...after about 2.5log2n times.)

However, I'd like to improve speed. Is there a built-in method or trick or something I have missed?

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm
Location: Göttingen, DE

Re: Creating a large Data of a specified length

Post by [-hh] » Sat Jul 20, 2019 4:28 pm

This needs at most log2(n) times "put after".
What's your handler? And what's your speed?

In LC Script this needs at about 1 second for 1 Gigabyte (2^30 Bytes) of data,
and < 20 ms for 17 Megabytes (2^24 Bytes) of data.

Here is the LC Script handler, the LC Builder handler could use the same "doubling-method".

Code: Select all

function createData m,n
  put numToByte(m) into b
  repeat log2(n)
    put b after b
  end repeat
  return byte 1 to n of b
end createData
shiftLock happens

DarScott
Posts: 227
Joined: Fri Jul 28, 2006 12:23 am
Location: Albuquerque
Contact:

Re: Creating a large Data of a specified length

Post by DarScott » Sat Jul 20, 2019 7:23 pm

Thanks! An adaptation of that will beat my recursive function.

I wonder if there is something like "MCDataCreateOfLength" among the built-ins. (I just made up the name.)

Post Reply

Return to “LiveCode Builder”