Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
1
Question by ina · Jan 22, 2014 at 08:39 PM · c#meshproceduralmethods

Mesh return vs out Mesh

is it better to use:

Mesh mesh = Mesh GenerateMesh(){ ... }

or

void GenerateMesh(out mesh)

ie get an output or pass in the mesh

Comment
Add comment · Show 2
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 ina · Feb 26, 2014 at 11:25 AM 0
Share

the answers below are conflicting... memory leak vs efficiency...

avatar image RudyTheDev · Feb 26, 2014 at 12:04 PM 0
Share

Performance-wise, this is generally needless micro-optimization. You are unlikely to be in a position where the difference matters. You really should consider code design and structure over saving a nanosecond. return is almost always the simplest straight-forward case, and out has no benefits when using a single output.

I'm not sure where the "memory leak" comment is co$$anonymous$$g from. You can make memory leak regardless if you do it with out or return. Objects don't leak unless you go out of your way to find a way to do it. The return example does not make a $$anonymous$$esh leak and I have no idea why they would think it does. This is like the most common syntax for functions returning object instances and I would think someone would have noticed if it leaked memory!

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Acegikmo · Feb 24, 2014 at 06:34 PM

I would guess it's more efficient and safe to use out or ref. If the variable mesh is already referring to a mesh object, the first example will them make a new mesh, and then assign it to the mesh variable, and thus there's a potential risk of object leaking. If you're using out, you can directly modify the existing one.

Comment
Add comment · 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
0

Answer by RudyTheDev · Feb 24, 2014 at 06:50 PM

It's almost always simpler to use return unless you have a specific use-case scenario. out is when you need 2+ returned variables and out for a single variable just complicates code. And ref is essentially for not copying value types and passing references/pointers (and Mesh isn't a value type, so it just "double-references" with no benefit). There is no performance benefit to using out (see comment). In fact, MSDN recommends not using out/ref.

Comment
Add comment · Show 1 · 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 RudyTheDev · Feb 24, 2014 at 06:56 PM 0
Share

Ran a quick performance test:

 for (int i = 0; i < 10000000; i++)
     a = $$anonymous$$ake$$anonymous$$eHello_Return(); // 361 ms (31 ms on empty for)

 private Hello $$anonymous$$ake$$anonymous$$eHello_Return()
 {
     return new Hello(3);
 }

versus

 for (int i = 0; i < 10000000; i++)
     $$anonymous$$ake$$anonymous$$eHello_Out(out a); // 582 ms

 private void $$anonymous$$ake$$anonymous$$eHello_Out(out Hello hello)
 {
     hello = new Hello(3);
 }

on a dummy class

 public class Hello
 {
     private int a;
     public Hello(int givenA) { a = givenA; }
 }

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.

Follow this Question

Answers Answers and Comments

20 People are following this question.

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

Related Questions

Procedural array of meshes problem / solved 1 Answer

Do I need to split my mesh up to properly UV texture it? 1 Answer

Trouble with Inaccurate Mesh Collider 0 Answers

How to subdivide a single face on a mesh? 0 Answers

Multiple Cars not working 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