Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
DAY 007: _PRESERVE
#4
(11-12-2022, 03:13 PM)mnrvovrfc Wrote: Curiously chosen QB64-only keyword. I was trying to point out to this:

https://qb64phoenix.com/forum/showthread...45#pid9745

It confused a couple of users. Maybe on a 64-bit system many memory block resizing requests in a short time could be handled well, but on 32-bit this was ornery. What I tended to do was read in the file twice (but it might have been even less efficient), the first time to discover how many elements to read in, then use "REDIM" for the first time and then read the file again. It had to be done that way without "_PRESERVE" and for those who didn't have M$ BASIC PDS v7.1. I was that afraid of making a program crash, and even when I started using 32-bit Freebasic because I had really bad experiences using 16-bit Power C for a year or so.

I've followed that approach as well, in the past, but don't tend to do so very often anymore.  Disk access and reading a file can take several seconds (or hours if one opens the file FOR INPUT).  As you can tell from my above demo, our REDIM _PRESERVE only tacks on a fraction of an second to resize most arrays.  When one is worried about loading times (such as when loading a large CSV database into memory, for example), it's often better to REDIM _PRESERVE in "large chunks", than it is to read the file for a line/element count, and then DIM once and have to reread the file a second time to actually read in the data.

It's honestly not very often at all anymore where I do the "read twice, dim once" method.  In fact, the method I often rely on the most is the "DIM stupidly large, read once, resize once" method.

REDIM array(1234567890) AS STRING
...read in data, count as I do so
REDIM _PRESERVE array(count) AS STRING 'redim down to the proper size so excessive amounts of memory aren't being reserved for no reason.
Reply


Messages In This Thread
DAY 007: _PRESERVE - by SMcNeill - 11-12-2022, 08:35 AM
RE: DAY 007: _PRESERVE - by Pete - 11-12-2022, 10:31 AM
RE: DAY 007: _PRESERVE - by mnrvovrfc - 11-12-2022, 03:13 PM
RE: DAY 007: _PRESERVE - by SMcNeill - 11-12-2022, 03:58 PM
RE: DAY 007: _PRESERVE - by Pete - 11-12-2022, 04:02 PM



Users browsing this thread: 1 Guest(s)