Question
Evgeny Shvarov · May 29, 2017

Globals vs Locals: What Is Faster?

Hi, colleagues!

Consider you need to put some (less  than 1GB) data to an indexed array and do some data manipulations and calculations with it.

Should I prefer global or local for it?

Is there any general rule when local is faster than global and vice-versa?

4
0 1,277
Discussion (5)1
Log in or sign up to continue

If you need faster IO but taking care with data size, I would recommend you to use local variables.
Memory IO is always faster than disk based storage.

That being said, 1GB is quite expensive to be allocated for a single process. Thus, for your case I would recommend using process private globals.

If you store data in globals, you can run parallel jobs on your data.

On a single job however, locals are faster because there's no journaling and disk writes.

You can use a temporay global ^CacheTemp or a global scope process ^||CacheTemp. You can get a good performance. Also you can use a regular global and turn-off jurnal for that specific job if you want a better performance and doesn´t care about that journaling process. 

I hope it helps

I would give you a list from fastest to slowest.

  • Local variables. Yes, this sort of variables is limited by memory, but currently, memory cost not so much and if you need only 1GB per server, it should be quite easy to get. By default, the process has only 256MB of memory for local variables, but you can extend it on a fly with special variable $zstorage, and maximum level is 2TB.
  • Process private globals (^||Global), it is already globals but works faster than any other types of globals, but slower than local variables. It is not limited by memory per process, but stored in memory, and could be moved to the disk if you exceed global buffer size.
  • Globals mapped to CACHETEMP (e.g. ^CacheTemp*, ^mtmp), such globals faster than usual globals because in this database journaling of any changes is switched off. 
  • Every other Globals, but still depends on is this database used journaling changes or if changes were made in a transaction. 

Just mount a Caché database on a RAM disk and take advantage :-) - easy to do and you can do a backup before you reboot.