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
0
Question by dansav · Aug 15, 2013 at 11:29 PM · slowtry-catch

why is try and catch ultra slow for this code

I am experimenting with try and catch in a browser (firefox) and also in unity editor. In a browser I can run 10000 of the try statements below in 30 seconds. Unity is running 400 of them in 30 seconds which seems extraordinarily slow. Why is unity so slow in this particular case? This is for a standalone build type working in the editor. If the try line is instead eval("x=2") then it runs quickly in under a second. eval by itself runs very fast. It's only the combination that is super slow.

 function testTryCatch(){
        var errors=new List.<int>();
 function testtrycatch(){
 for(var i=0;i<400;i++){
 try{
     eval("x=y");
     }
 catch(e){
     errors.Add(1);
 }
 }
         Debug.Log(errors.Count);    
         }
         
     testTryCatch();

Comment
Add comment · Show 4
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 trs9556 · Aug 15, 2013 at 11:32 PM 0
Share

Is script debugging on when you use the browser?

Perhaps the stack trace isn't being generated in the browser but in unity it is? (This assumes there is an error)

avatar image OP_toss · Aug 15, 2013 at 11:52 PM 0
Share

Ew... eval is gross!

Is there a reason you're using it? Hate to be that guy that doesn't help and says you're doing it wrong, but you maay be doing it wrong. Unless you're hacking something or are forced to by some strange plugin.

Evaluating strings to code is a big no-no in my book. It only follows that it's slow, but I agree, it maybe shouldn't be as slow as you say.

Hope this helps!

avatar image dansav · Aug 16, 2013 at 12:18 AM 0
Share

script debugging is on in the browser. I don't think it's the eval that's slowing it down but I can check that also.

avatar image dansav · Aug 16, 2013 at 12:40 AM 0
Share

It's not eval causing the problem by itself. 400 evals runs very fast.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Eric5h5 · Aug 16, 2013 at 12:44 AM

Unity only runs compiled code, so it has to compile the eval() code on the fly, which is fairly slow. On the plus side, once eval() code is compiled, it runs fast, but you have 400 different strings that have to be compiled.

Also, it doesn't help that you're using dynamic typing. It's really better if you forget that Unityscript is officially called "Javascript", and don't treat it like web Javascript at all. Don't use Array, and type all variables. Pretend you're writing ActionScript3 code instead, and you'll do much better.

Comment
Add comment · Show 5 · 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 dansav · Aug 16, 2013 at 03:35 AM 0
Share

I changed the code to get rid of dynamic typing. If eval is correct like eval("x=2") it runs really fast like in under a second.. If eval is wrong like eval("x=y") it runs so that 400 of them take 30 seconds or so.

avatar image Eric5h5 · Aug 16, 2013 at 03:50 AM 0
Share

O$$anonymous$$, so it was mostly the dynamic typing that was the problem, rather than the eval() compiling? I wouldn't have expected that.

avatar image dansav · Aug 16, 2013 at 04:52 AM 0
Share

No. It's still slow but only if eval has a mistake. Otherwise it's fast. Is there any reason for that?

avatar image Eric5h5 · Aug 16, 2013 at 04:59 AM 0
Share

Uh, mistakes are bad I guess? ;)

avatar image dansav · Aug 16, 2013 at 05:26 PM 0
Share

I'm trying to let the user input some unity code then check it for syntax errors as part of a learning unity project. The speed will matter if they have many lines of code. Is there another way to do something like that?

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

18 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

Related Questions

Unity doesn't like generic stacks built through recursion?? 1 Answer

Game skips at specific points in level (not garbage collector) 0 Answers

What can i do about my unity webplayer? 0 Answers

Unity game started running slow 3 Answers

Eliminate CPU spike from shader load? 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