• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
Question by Essential · Apr 04, 2012 at 12:49 PM · variablestaticlocalglobalvar

Should I create local copy of global variable?

I've got a little script that tracks player input, which can be accessed in other scripts via global variables. Is it fine to just keep calling e.g., ControlInput.movement multiple times in those scripts, or is it better for performance to copy the global variables into a local ones for the purposes of that script?

Sorry, bit of a noob question. It might make no difference at all. Just trying to learn best practices. =]

Comment
aldonaletto
Datael
cregox

People who like this

3 Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image cregox · Apr 04, 2012 at 10:56 PM 0
Share

That's odd. If anything, I'd guess static variables perform better than local.

avatar image aldonaletto · Apr 04, 2012 at 11:18 PM 0
Share

@Cawas, you may be right: according to the MSN docs, the instructions used in member variables (Ldfld and Stfld) receive an indirect reference to the variable (a pointer to a pointer to the variable), while the instructions that handle static variables (Ldsfld and Stsfld)) receive a direct reference (a pointer to the variable). This makes a negligible difference, of course, but static seems a little (very little) faster.

avatar image cregox · Apr 05, 2012 at 01:31 PM 0
Share

Cool @aldonaletto ! I know nothing about those "instructions" but... Does it being slightly faster have anything to do with static needing no instantiating compiler-wise?

3 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by aldonaletto · Apr 04, 2012 at 03:20 PM

Not a noob question - on the contrary, that's a very interesting one! I was just wondering myself about this same subject, and decided to use ILSpy to check the CIL code generated for static and member variables.
It seems that there's no gain in caching static variables: both, member and static variables are accessed with a single instruction - ldfld for member variables, ldsfld for static ones. These instructions are interpreted and executed in native machine code, but for sure there's no significant difference in CPU cycles - it would be a lot different if several CIL instructions were needed to access static fields, what's not the case.
You can see below the significant part of the simple JS code converted to CIL, with comments linking the CIL instructions to the JS ones:

Script CheckVPort.js:

 static var vStatic = 0;

Script Zoom.js:

var vLocal = 0;

function Start(){ vLocal += 1; CheckVPort.vStatic += 1; } The same Zoom.js compiled to CIL:

//var vLocal = 0; .field public int32 vLocal

//function Start(){ .method public hidebysig virtual instance void Start () cil managed { // Method begins at RVA 0x88e0 // Code size 27 (0x1b) .maxstack 8

     IL_0000: ldarg.0
     IL_0001: ldarg.0

// vLocal += 1; IL_0002: ldfld int32 Zoom::vLocal // read int variable vLocal... IL_0007: ldc.i4.1 // load constant 1... IL_0008: add.ovf // add constant to it... IL_0009: stfld int32 Zoom::vLocal // and store in vLocal // CheckVPort.vStatic += 1; IL_000e: ldsfld int32 CheckVPort::vStatic // read int static vStatic... IL_0013: ldc.i4.1 // load constant 1... IL_0014: add.ovf // add constant to variable's value... IL_0015: stsfld int32 CheckVPort::vStatic // and store in vStatic IL_001a: ret } // end of method Zoom::Start //}

Comment
Datael
Eric5h5

People who like this

2 Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Kryptos · Apr 04, 2012 at 06:28 PM 0
Share

Well actually, the fact that there is only one CIL instruction does not mean it is the same number of CPU cycles. And CIL instructions hide VM optimizations such as caching and registering.

But as you pointed out, the difference might be too thin. It is better to put all effort on algorithm and data structure than on this kind of subtleties.

And I agree: not a noob question ;)

avatar image Essential · Apr 04, 2012 at 07:15 PM 0
Share

Awesome, great to know. Thanks. =]

avatar image aldonaletto · Apr 04, 2012 at 09:40 PM 0
Share

@Kriptos, you're right: maybe "execution time" would sound better than "CPU cycles". In fact, the fetching, interpretation and other jobs executed for each CIL instruction often take more time than the actual machine code - a price to pay for the safety and portability of CIL code. But the main objective was to check if member and static variables were accessed by similar instructions, what they in fact are (even the instruction sizes are the same, 5 bytes).

avatar image

Answer by Kryptos · Apr 04, 2012 at 02:16 PM

From a conceptual point of view, this makes no difference. From a more practical point of view, this is harder to tell. Local variable may be on the stack, i.e. very close to the code using it. On the other hand variables from other script can also be cached into registers.

Although C# (or JS) runs on a virtual machine (Mono), these concepts are pretty much the same as code running on a CPU (such as C/C++). But this depends exclusively on the implementation and design choice of the VM. As game developer we can't do anything about it.

Regarding games made with Unity, I don't think it's making any difference. You don't have the same constraints as realtime-guaranteed applications. So just use the way that fit your coding habit.

You can do much better optimization by choosing the right algorithms and data structures.

Comment
Essential

People who like this

1 Show 0 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image

Answer by clfischer · Apr 04, 2012 at 01:49 PM

I've read that it's a lot faster to access a public variable from another class directly (ControlInput.movement) than to make it private and call an accessor function (ControlInput.GetMovement()). Not necessarily as tidy in terms of encapsulation but faster. However, I don't know if there's any difference between getting a variable from another object or from the current object. I expect it performs pretty much the same.

Comment

People who like this

0 Show 0 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Increase Static Var gradually 1 Answer

Unable to modify a variable in another script 2 Answers

How to use same code at multiple scenes ? 1 Answer

How to make a var "global" 0 Answers

Quick question about global variables. Two ways. Are they different? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges