Home   Help Search Login Register  

Author Topic: which costs more...  (Read 757 times)

0 Members and 2 Guests are viewing this topic.

Offline bedges

  • Administrator
  • *****
    • OFPEC The Editing Center
which costs more...
« on: 15 Oct 2005, 10:52:00 »
it's maybe a pedantic question considering how fast flashpoint parses scripts, but i'm wondering which is more efficient:

  • defining 6 global arrays to be used by a handful of scripts in the init file;
  • defining those 6 arrays locally in each script which needs them.

they're arrays of all weapon types and corresponding ammotypes, so they're quite long. they're used by 2 scripts so far.
« Last Edit: 15 Oct 2005, 10:52:37 by bedges »

Offline THobson

  • OFPEC Patron
  • Former Staff
  • ****
Re:which costs more...
« Reply #1 on: 15 Oct 2005, 12:20:08 »
I always use local arrays where I can and global only when necessary.

I believe that the time taken to parse a script is irrelevant to performance unless there are thousands of them that are parsed in a short space of time

Offline Trapper

  • Honoured Contributor
  • ***
  • I'm a llama!
Re:which costs more...
« Reply #2 on: 15 Oct 2005, 12:26:19 »
maybe...

Global
 good
 - faster access
 - lower chance of typos
 - later changes are more easy
 - array content can be changed in game by one script for all scripts (global ;) )

 bad
 - always loaded in memory

Local
 good
 - only loaded in memory if needed
 - each script on it's own is easier to understand

 bad
 - same array multiple times in memory if scripts are running simultaneously
 - slower access
 - later changes are more difficult
 - higher chance of typos
 - array content can't be changed in game by one script for all scripts (local ;) )
« Last Edit: 15 Oct 2005, 12:48:44 by Trapper »

Offline bedges

  • Administrator
  • *****
    • OFPEC The Editing Center
Re:which costs more...
« Reply #3 on: 15 Oct 2005, 12:59:38 »
hmmm.... how d'you know local arrays are accessed slower than global arrays?

Offline Baddo

  • Former Staff
  • ****
  • Reservist Jaeger
Re:which costs more...
« Reply #4 on: 15 Oct 2005, 13:29:50 »
hmmm.... how d'you know local arrays are accessed slower than global arrays?

My guess: because a global variable initialized in the start of the mission is already in memory when your script starts but a local variable needs to be initialized first in each script (placed into memory). And additionally, the speed to access array contents after the variable has been placed into memory can also be different for local and global but this can't be said without testing it and it's not what matters here.

It's a guess only from my part. This is the kind of things which can be said for sure only through testing.

I would say, make it global but remember to use your ofpec tag in the variable name.

THobson has a good point there to avoid global variables as much as possible. It's a normal programming way to give the variable a scope it needs, but not any more. In OFP we have to make exceptions sometimes (your global variable in OFP can be accessed from other scripts which don't actually need to access it at all).
« Last Edit: 15 Oct 2005, 14:03:39 by Baddo »

Offline Trapper

  • Honoured Contributor
  • ***
  • I'm a llama!
Re:which costs more...
« Reply #5 on: 15 Oct 2005, 13:41:29 »
I don't know it, but I had the same thoughts as Baddo.

Offline bedges

  • Administrator
  • *****
    • OFPEC The Editing Center
Re:which costs more...
« Reply #6 on: 15 Oct 2005, 13:45:56 »
right. some good advice there gentlemen, thankyou  8)

Offline THobson

  • OFPEC Patron
  • Former Staff
  • ****
Re:which costs more...
« Reply #7 on: 15 Oct 2005, 15:54:12 »
Test it with several thousand arrays and see if you can tell the difference.