Interesting results! Whenever I teach optimization, I always begin with "Measure, don't guess". Measure for your platform. Measure in real world situations. Start with readable code, measure and then fix to meet a frame budget. Everything else is premature optimisation because you can't guess what optimisations the compiler will do.
@Tarodev2 жыл бұрын
I like this and fully agree.
@danebramage4522 жыл бұрын
There's a saying... you get what you measure.
@saniel27482 жыл бұрын
You can pretty much always guess that arrays iteration is faster than linked list. And pretty much guess that GetComponent in Update is kinda bad
@BDCPT-NguyenThanho2 жыл бұрын
at the example between Vector3.Distance vs SqrMagnitude have u try ((Random.insideUnitSphere - Random.insideUnitSphere) * MULTIPLIER).sqrMagnitude || i think it is a bit faster || 1 multiplier and 1 subdivisor faster than 2 multiplier and 1 subdivisor right ? (this is my opinion)
@beanieteamie7435 Жыл бұрын
In my case I'm working with a compiler that literally doesn't optimize anything, this creates code that runs several orders of magnitude slower than a regular monobehavior.
@r6scrubs1262 жыл бұрын
Love to see actual tests instead of people just repeating the same things they heard elsewhere. Good job
@alexey6343-x02 жыл бұрын
"Actual" means on different devices with different CPU architectures and different compiler options?
@ЛеняБорисов Жыл бұрын
your clients (means gamers) are playing in unity editor? If they so, you could apply on that "tests". Anybody actually developed at least one game says that you must check optimization in release build on device only
@avgchoobafan2 жыл бұрын
Really nice insight on code performace! I would recommend having an "average" result for each test in miliseconds, so the more you test you get a stable number to compare.
@Tarodev2 жыл бұрын
Damn! Why didn't I think of that. Great suggestion
@sdhority2 жыл бұрын
The order of operations bit is really handy, it's really impressive to see the difference that just reorganizing your math can make on computing time. Thanks!
@CheeseChuckie2 жыл бұрын
I love a good caesh! Speaking of - I've heard the idea of a "Runtime Set" in a few talks regarding scriptable objects. I think it's a very nice alternative to finding objects by/of
@bonehelm2 жыл бұрын
Regarding the Vector3.Distance function, you're also making 2 calls to Random.insideUnitSphere, which definitely makes 1 call to sin and 2 to cos, which are also both expensive operations. But then you're calling it twice. The 6 calls to sin/cos probably dwarf the time it takes to make 1 sqrt call. That's probably why sqrt and sqrtSquared about the same time, cause the 6 trigonometric computations are taking over.
@Tarodev2 жыл бұрын
You're right. The simplified code I posted on screen shows more predictable results, but even then... not enough for me to use the less readable option. (900k iterations, 6ms and 4ms)
@television10882 жыл бұрын
Also you should be comparing it to MIN_DIST*MIN_DIST
@davibergamin59432 жыл бұрын
@@television1088 if we cache MIN_DIST*MIN_DIST it will be the same thing
@television10882 жыл бұрын
@@davibergamin5943 That's not something that would ever be cached except for artificially improving benchmarking.
@vizepi692 жыл бұрын
@@Tarodev Your simplified code is still hiding the call to operator- which *may* have an impact on performances, even if Vector3 is stack-allocated, it can trigger a call instruction to operator- and constructor. By manually writing a Vector3.SqrDistance( Vector3 a, Vector3 b ) you can have a ~40% gain in performances
@ronygankin41582 жыл бұрын
Hey Tarodex, avid fan here. I've been studying game dev in a local college here (its unlike US colleges) and while we learned alot of game oriented programming, your more "classic" programming stuff is just enormously helpful because you stay so relevant to video game programming. Really wanted to thank you for that, it's super enriching and I learn alot from your videos. If you ever make a series that's more in depth programming then getting components while still staying relevant to video game programming. You have a fan waiting. Thx alot for sharing all that knowledge my friend!
@lastsipahi Жыл бұрын
Last one completely caught me off-guard. It'll be hard to erase a 10 year old habit! Great video btw, thx.
@treppas2 жыл бұрын
Super interesting! Thanks for taking the time to put this together and share it 😊.
@yerngames2 жыл бұрын
Great video! Just remember everyone, optimization is the last thing to do! Do not try to optimize everything that you make at the start. I always suggest to make a sloppy prototype and re-write an optimized version of whole code later on.
@Tarodev2 жыл бұрын
I cannot heart this comment enough. I was planning on adding a prefix to this video regarding pre-optimization but I guess it slipped my mind.
@yerngames2 жыл бұрын
@@Tarodev Thanks for the heart :P
@umapessoa60512 жыл бұрын
I disagree, its the same as doing multiplayer games, its something that is easier/faster/better to do since the beginning.
@RobLang2 жыл бұрын
@@umapessoa6051 I disagree. It's not the same. Multiplayer sets architectural constraints on your code, so you should do at start. Optimization should be as a result of measurement. You cannot guess what the compiler will do.
@umapessoa60512 жыл бұрын
@@RobLang when your game grow big enough its pretty hard to change the base of your code to optimize it, you'll probably lose a lot of time doing it, you can't predict what the compiler will do but you can know for sure that some functions/ways of doing things shouldn't be used at all as there's more optimized options.
@szyslay2 жыл бұрын
SUCH a gem of a video! Currently makiny my game on Early Access and this helps. Glad to use all recommended functions already ^_^ Thank you for sharing!
@MalbersAnimations2 жыл бұрын
You my friend, have earned the bell notification button... well done 👏👏👏👏
@Tarodev2 жыл бұрын
Oh wow. Thank you ❤️
@Iboshido2 жыл бұрын
Love this type of videos. Code optimization in Unity isnt covered enough on KZbin
@zoks2 жыл бұрын
Release builds will have vastly different perf than what you see in the editor (debug). Great vid.
@eduardo-silva-792 жыл бұрын
Certainly didn't know a lot of these and they are really useful! The time complexity test also helps to understand your point quite a bit more, rather than simply trusting your word.
@kingreinhold99052 жыл бұрын
This unity scene is so good for teaching these optimisations... Great quality content as always! Keep going, thanks for your work!
@VEOdev2 жыл бұрын
This channel faster became my favorite game dev channel with many useful information that no other dev talking about and things I didn't really know they exist or how to use them .. I would like to see a Playfab tutorial on multiplayer game and matchmaking and stuff it will really help since there isn't any one talking about it and who talks about it only shows the basics but no one goes deep in it and there is non useful documentations
@Tarodev2 жыл бұрын
I did a recent one on LootLocker, but it's not a step by step tutorial, more of an overview.
@YasserSedrati2 жыл бұрын
You did all what we was wondering about. Thanks for this video! -good UI scene btw.
@Tarodev2 жыл бұрын
Better than looking at some boring numbers in a console :)
@hasankarakoc15762 жыл бұрын
I was just researching for it and you made it just in time! Thanks!
@overrideFunction2 жыл бұрын
Being new to Unity this was fantastic. It also provided some context to how expensive calls actually were. Needless to say I will avoid the game object and type find calls like crazy!
@badscotsman2 жыл бұрын
Great video! You hit on all my potential talking points concerning usage. 😊 Another one you see a lot is using CompareTag() vs using the == operator. Speaking of, how Unity overrides the == operator is another subject worthy of a video. 😅
@Tarodev2 жыл бұрын
Whenever I can please you, I know I've done alright 😊 And dammit! Compare tag would have been a great one to showcase
@freddyspageticode2 жыл бұрын
Wait so which is faster?
@BenVanTreese2 жыл бұрын
@@freddyspageticodeCompareTag is much faster. As well as you should avoid object.name where you can, as it crosses C++ boundary AND allocates. (i.e. if(obj1.name == obj2.name))
@Handlessuck12 жыл бұрын
@@BenVanTreese Why would c++ be slower if its going to become assembly?
@BenVanTreese2 жыл бұрын
@@Handlessuck1 It depends on what you mean, generally C++ is faster. Both C# (when using IL2CPP), and C++ are compiled to native instructions, but due to the general nature of C#, when it "talks to" the C++ portion of the engine itself, it needs to do some marshalling and etc. (C++ doesn't really become assembly, it is compiled to native instructions, which we write as ASM so people can read it, but ASM is just another language that is converted to actual bin instructions, which are defined by the specific arch of the CPU/target) This causes it to do extra work + allocate memory that needs to be GC later. CompareTag is optimized where it does the work in the "native" part of the engine without requiring the marshalling and etc.
@SlyzDev2 жыл бұрын
Bro. Im actually watching all your videos and they all are just too good for someone like me. Like seriously. I never comment under videos but like litteraly what you show is so damn useful lmao thanks for everything
@odo4322 жыл бұрын
In regards to the NonAlloc, I've done some testing with this and it can yield significant performance improvements depending on how you use it. For example, if you perform a OverlapSphere and there is 100 colliders then it will process 100 colliders. However, if you use OverlapSphereNonAlloc and set it to 10 then it will only process 10 colliders and disregard the rest. This can be handy, for example, if you want to improve performance at the cost of accuracy. EDIT 1: Just had a go at your WEBGL build. Using Vector.Distance is almost twice as slow for me with 900k loops (12ms for Distance vs 7ms for Square Root). EDIT 2: Just had a go with the order of operation. Even with 900k loops I would almost always get 6ms for each one. But every now and then FxFxV would jump to 9ms whilst the other two would stay on 6ms or jump to 7ms. For the most part, I don't think order of operation has a significant impact.
@Tarodev2 жыл бұрын
The NonAlloc collider limit is great. I use it for grounding and use an array of size 1. As for the results... It seems WebGL is just insanely varied from pc to pc and browser to browser. My webgl distance comes out to 6ms and 4ms, so sqrMagnitude wins, but not by enough to not use the far more readable Vector3.Distance. Order of operation comes out identical for me on both webgl and il2cpp.
@FaDe5872 жыл бұрын
@@Tarodev I would guess that Order Of Operation might be one of those things the compiler can detect and optimize away.
@Tarodev2 жыл бұрын
@@unitydev457 that's actually a test I'd like to run... If there's 1000 colliders and you limit it to 10, how much faster is that
@okee2 жыл бұрын
Code optimization is one of the things I planned to improve this month. VERY BIG THANKS!!
@bxbvxbv2 жыл бұрын
finally I can focus on just writing simple working code rather than constantly stressing by thinking about ways to make it performance efficient. the amount of emphasize regarding using some functions in docs & web is too much, that if you do this then it's slower. thanks for making this awesome test 🔥
@TheIcyGoose2 жыл бұрын
Thank you for an excellent video! I would be interested to see you run these tests in a build and not in the editor. I find the performance is usually different once you build and run.
@FyresGames2 жыл бұрын
The Linq vs For was one I was looking for. I was annoyed by a friend refactoring my for loops into Linq behind me because "Cleaner". It made my day to see that concrete proof I was right.
@daniel_keen10 ай бұрын
I did not know about the order of operations. Very interesting!
@jacques-dev2 жыл бұрын
(cries in cache) Thanks for the great video dude, I'd love to see more videos like this with more unity specific things, like nesting GameObjects vs. having them live free and wild in the hierarchy, nesting canvases, and other best practices for performance
@simoncodrington11 ай бұрын
Fascinating, never knew transform wasn't a direct property and that behind the scenes it was going off and doing stuff, another quick win for caching
@Spleegness12 жыл бұрын
God DAMN this is exactly my kinda video. You're incredible!! Thanks for the hard work
@FaDe5872 жыл бұрын
Regarding the Vector Distance thing: It is important to note that Vector3.Distance and sqrMagnitude do NOT produce the same result. The result of Vector3.Distance produces the magnitude of the direction vector between the supplied vectors whereas sqrMagnitude is that same magnitude squared. This can be useful if you want to compare distances, e.g. to see which of two positions is further away, because you don't really care about the actual distance but rather which of the two is greater. If you however need to get the actual distance, you have to use either Vector3.Distance or magnitude, sqrMagnitude will be incorrect.
@DevDunkStudio2 жыл бұрын
This one is great to know for performance and not many use this properly: Caching WaitForSeconds and other yield return calls (like WaitForEndFrame etc) in a coroutine! You can cache the variable as private WaitForSeconds = new WaitForSeconds (1f); Without doing this you'll add stuff to the garbage collector every so often which can add up! Bonus nitpick: you can change both transform.rotation and .position by calling setPositionAndRotation, then the calls are merged into 1?
@Tarodev2 жыл бұрын
I remember years ago when I first decompiled WaitForSeconds to discover it being a class and not a struct. I was shocked and began caching them ever since. Nice tip! Also yup, just decompiled setPositionAndRotation and it is indeed just one extern call.
@AJ213Probably Жыл бұрын
Be very careful with this. You may be tempted to make central area with different cached wait for seconds. Or maybe you would make a dictionary for caching this too. The problem with that is if multiple coroutines are using the same wait for seconds, the timing will be completly wrong due to the internal timer being reset when the routines exit. I find this often at my job.
@t33h33studio5 ай бұрын
I always knew FindObjectOfType was on the slower side, but that is still surprisingly slow from what I was thinking. Good stuff!
@nodaichi60802 жыл бұрын
i have no idea why, but hearing your voice just makes feel soothed.
@Tarodev2 жыл бұрын
Awwww ❤️
@gamesplusjames2 жыл бұрын
This is really interesting to see! I've been talking about a few of these recently so it's good to have a clear example to point to!
@KojiKazama2 жыл бұрын
Thank you sir. I will need to make a quick reference for these. The last one was unexpected as I didn't think the order actually mattered.
@zerohidz2 жыл бұрын
I havent watched the video yet but I'm sure it will enhance my knowledge and benefit me. Thanks, please keep this quality content.
@cgh3532 жыл бұрын
this is useful. thanks. I would like to see you test optimization on more code.
@sinisterdesign2 жыл бұрын
Great video! It's especially good to see the StringBuilder hype confirmed. I'd love to see the performance of the string Contains() method compared to alternatives like IndexOf(), RegExp, and Linq.
@francoismentec42602 жыл бұрын
I LOVED this video, I just started Unity and you are my favorite channel so far. Quality content focused on Unity (not generic programming stuff) and Game Dev. I did not even knew about Unity making external call to C++ I would be interested in a follow up video focusing on differences across platforms. Maybe look at Browser vs Windows vs Mobile.
@divainsyoutube42542 жыл бұрын
Great video! Haven't known them all so thanks for the knowledge! About the cached for-loop, I'll add and say that it is faster because the non-cached version, List.Count invokes the get method on the Count property each iteration
@danleg12 жыл бұрын
Super helpful tests, would love to see more this
@yudanaim68492 жыл бұрын
Awesome video. Im waiting for ECS comparison
@CrazyMan-lt6ky2 жыл бұрын
Man, you're rocks! Thank You for your course !!! I've learned so much!!!
@lee1davis12 жыл бұрын
Order of operation is the only one I did not know about. Glad you added it too.
@AndersonReis42 Жыл бұрын
"send message is not a good practice" and here I was sitting and thinking i was doing a neat job using send message to optimize my code. thanks for the tip!
@synchaoz2 жыл бұрын
Dude, yes, love vids like these and you're like the only one on the planet doing them. Much love.
@TheOriginalDarkGlitch2 жыл бұрын
You are doing the Lord's work. Thank you for being awesome, I always wanted to do tests like this, but I don't have time right now.
@jonathansaindon7882 жыл бұрын
Nice video! For order of operations, you can achieve the same result by grouping the same types in parentheses like : Vector3 stillFast = x * (a * b).
@Nova045502 жыл бұрын
Good idea
@zenmasters_games2 жыл бұрын
Bro, you deserve way more subs! Cheers 🍻
@Tarodev2 жыл бұрын
Thanks brother
@dzj3zterminator6612 жыл бұрын
eventually it all snapped into place and I started learning how to add all the effects, titles, motion text. It was pretty cool to see my
@jahoopyjaheepu4972 жыл бұрын
This is a great video, and some of your results make me feel better about optimizing some of my code; it can actually make a difference! In regard to NonAlloc methods, I believe the Unity 2022 documentation states that they will be deprecated. Instead, something like Physics2D.Boxcast has two overload methods - one that takes an array as a parameter, like in your example, and another that takes a List - that actually grab all colliders, not just the first one. I would be curious how these perform, and especially if the List version is notably slower and/or produces any garbage.
@Tarodev2 жыл бұрын
That's interesting... I do prefer how the NonAlloc version works as it returns a count also. Good change IMO.
@rickloyd82082 ай бұрын
Very interesting, thanks for your work and sharing results with us!
@longroadgames6592 Жыл бұрын
I just found your channel and I love it. Thank you!
@testitestmann88192 жыл бұрын
The sqrMagnitude really surprised me. I made sure to always avoid the sqrt calls. Thank for checking!
@XinvaderGaming2 жыл бұрын
The comnt section is very positive and downright encouraging! Love it!
@Rubidev2 жыл бұрын
It`s intersting that despite my thoughts that unity tuorials are saturated, you still manage to surprise us with unique videos. Very intersting level of expertise. I salute you!
@nirangasithara2 жыл бұрын
Damn !!! it's like the first day learning programming for me. Hope in few years I can understand and utilize these !!
@user-uk9er5vw4c Жыл бұрын
this is awesome, I love this kind of low level things to improve code. thanks and you're welcome to do more videos like this
@svendpai2 жыл бұрын
been looking for a video like this, taro you champ
@dathus64372 жыл бұрын
If foreach is faster in your tests, that's actually because it is implemented on a different assembly which has been compiled to release mode, whereas your for loop is in build mode which is generally way slower. This is why when compiled in webgl the for loop is actually faster!
@lanepemberton84662 жыл бұрын
I was wondering how a for loop could be slower than a foreach as well
@nikoart15612 жыл бұрын
And actually foreach generate more garbage Collection
@iDerp692 жыл бұрын
This is great! Hope you make more of these optimization tests in the future.
@PvPNetwork2 жыл бұрын
Thanks for sharing. Even if you have an idea of what is potentially better its nice to see some testing results. The small things like VxFxFx make sense if I think about it - but I doubt I would have even thought about it until you mentioned it. Remember using strings in my item tooltip , and switched over to stringbuilder - never looked back!
@halivudestevez22 жыл бұрын
thank you for mythbusting! :) I am happy you've made these clear! (SqrRoot -Distance)
@animal96332 жыл бұрын
Thanks for that last one, I've never seen that anywhere before.
@Fewnity2 жыл бұрын
Great video! Thanks, I was surprised with the results!
@qwertyram45982 жыл бұрын
About the distance, Godot has a separate function, squareDistance, which gives you the square of the distance(as the name implies, it doesn't root the distance). The documentation says use this if you're comparing two distances, as it's faster.
@Tarodev2 жыл бұрын
Godot has some lovely little helper functions it seems
@bonehelm2 жыл бұрын
@@Tarodev the reason you're not seeing a difference between distance and distanceSquared is because you're also calling Random.insideUnitSphere twice. That function calls triginometric functions 3 times, so you're doing 6 trig computations. So 6 trig computations are going to dwarf the call to 1 sqrt.
@Norbingel2 жыл бұрын
I learned more than a thing or two. Thanks!
@briankesecker2 жыл бұрын
Great video and app to show off these things. Would like to see another video with it running outside the editor on a smattering of target platforms.
@WagnerGFX2 жыл бұрын
Whenever i think some code could have a performance impact I do this type of test, but a lot simpler. The whole setup for benchmarking is great and I will use it in the future to test logic faster. 👍 I just wished there was an easy way to track GC allocations too.
@YoutubePremiumMem2 жыл бұрын
The profiler is decent at telling you when gc is collected and how much but it’s a bit crap at actually helping track down the culprit and so it needs a lot of drilling down to find the offender.
@shreyanshanchlia2 жыл бұрын
Would like to see interpolated string performance added in the string builder list.
@NicTda2 жыл бұрын
Very interesting, mainly confirming what I was finding online to optimize our games. Regarding the last point, the Order of operations, it seems to have 0 impact on WebGL build when running your benchmark, probably because it's optimized at compile time (3ms for 900k on all tests). Good stuff!
@this-is-gamedev2 жыл бұрын
Awesome video 👍! Did not know about the order of operation ! Now, need to refactor all my code XD.
@Tarodev2 жыл бұрын
Please don't. I just tried 900k iterations on a standalone build and they all came out even. It's so tiny to not even matter :)
@JhonatasFarias2 жыл бұрын
Loved it! Do more videos like this, please! Frame Debbugger... Profiller... Physics.. would be awesome♥
@soverain2 жыл бұрын
Very interesting! Edge case for SendMessage: DLL plugins. I recenttly had to implement a iOS native camera access permission plugin and the simplest solution to get communication going was to use SendMessage.
@Tarodev2 жыл бұрын
Oh yeah? Were interfaces a no go?
@GameDevCoffeeBreak2 жыл бұрын
Nice work, thank you a lot for this video!
4 ай бұрын
I couldn't overcome all the thread performance bottleneck and issues and started coding in ECS and everything is now so smooth and performant. Still some cases are really harder to code than MonoBheaviour, but it worths when I see the performance difference. Using Burst compiler with JobSystem and run everything parallel when possible gets a lot of performance advantage. This is similar coding with C++ but you still use C#. Interesting approach but it works pretty smooth
@PapaGoras3 ай бұрын
I started DOTS programming too in Unity and yes the performances are wayyyyyyy better (far away) but have to learn everything again, not easy !
@torcheyegames2 жыл бұрын
Great video. Actually the newest Rider has the last bullet point covered in their editor - it will give you a hint of "expensive computation" or something like that and you can one-click to change the order of multiplication, like from V*F*F to F*F*V.
@Tarodev2 жыл бұрын
I've quite enjoyed that new feature
@TheKr0ckeR2 жыл бұрын
wow, great video! always stressed about optimizations
@kilroyyyyy902 жыл бұрын
That last one is HUGE for my game, literally just what I needed haha There's quite a hefty load in FixedUpdate just because of the nature of the game, but I've been doing essentially V*F*F for everything just because it makes more sense to do that in my head. But I shall change my ways :D
@AntonioNoack2 жыл бұрын
and even better, don't write it like v*f*f but instead with brackets, v*(f*f), so your future self will understand what you have done 😄
@Nova045502 жыл бұрын
Super helpful!! You put so much thought into this. You are killing it.
@Foxyzier2 жыл бұрын
Really liked this format for the video
@Warwipf2 жыл бұрын
3:53 that is super surprising to me. thanks.
@marcoseliasmep Жыл бұрын
Awesome! Another tip that could make a huge impact is avoiding Update, FixedUpdate and LateUpdate in scripts that have many instances loaded. If you do a foreach… myscript.CustomUpdate in thousands objects it will be faster than having Update() in all thousand instances. I just avoid using it in my project because we must have a list of all objects. With dynamic worlds and spawners based on distance that array would be constantly resized, so generating garbage. But with a fixed amount of enabled objects the change could save a lot of milliseconds.
2 жыл бұрын
Very nicely put video ! Good job.
@steviewee892 жыл бұрын
An interesting point about the NonAlloc example. If you reduce the size of the preallocated array it actually does become reliably faster. I'm assuming that internally it is spending time clearing the array every time the method is called.
@Betruet2 жыл бұрын
Phat info dump, thanks for putting in the effort.
@pixelshenanigans15112 жыл бұрын
Useful comparisons, thanks for the video!
@doismilho2 жыл бұрын
Best dev channel currently on youtube
@Nullref_Arcana2 жыл бұрын
Regarding FindObjctOfType, I actually made a component that I use regularily called Fetchable, which has a string id/name field and a static dictionary. Then you can do Fetchable.Fetch(idName) to find the specific object. This of course makes it a one-to-one name to object, but I find it as a really useful alternative to any Find method (assuming it's a 1-to-1, which is often the case)
@animeshow28332 жыл бұрын
Can't wait to see your other videos.
@chris_gamedev2 жыл бұрын
Love to see someone test these in the opera gx and brave browsers :D
@Tarodev2 жыл бұрын
That would be very interesting. Sounds like a job for you sir!
@gmunay4202 жыл бұрын
I feel like this video and the one previous about Object References, were made directly for me. I have given up caching for the time, i just want to get things working b4 i make it work well. I was confusing myself trying to remember am i calling; Darg, or drag, or _drag. I will totally be referencing these videos when its time.
@insidiousmaximus2 жыл бұрын
excellent content so glad your channel found me
@FireJojoBoy2 жыл бұрын
This is super helpful! thank youuu!
@r1pfake5212 жыл бұрын
I read somewhere that they store objects with tags in a extra lookup, so they only have to iterate over that one instead of iterating over overy object, so it makes sense that it is much faster
@pstudioDOTtk2 жыл бұрын
Interestingly when I try the WebGL build FxFxV is the slowest in the order of operation test. For max iterations the most frequent result was 4,3,3. Distance was also slower than squared magnitude. It just shows how difficult optimisation can be and that you should probably test on multiple machines and not just your own.
@Tarodev2 жыл бұрын
So many people reporting skewed results in WebGL. We're at the mercy of the browser!
@KDSBestGameDev2 жыл бұрын
I hope it is fine for me to explain something on the Vector3.Distance thing. Vector3.Distance doesn't instantiate a new Vector3, but your calculus for (b - a).sqrtMagnitude does. So this is not a fair comparison. If you compare (b - a).sqrMagnitude with (b - a).magnitude then there is a 2x performance difference. If you remove the memory allocation completly and compare it with Vector3.Distance it is even slower. My example Code: Vector3 z = Vector3.zero; float MULTIPLIER = 500000; int c = 100000; var sw = new System.Diagnostics.Stopwatch(); sw.Reset(); sw.Start(); for(int i = 0; i < c; i++) { var value = (UnityEngine.Random.insideUnitCircle * MULTIPLIER - UnityEngine.Random.insideUnitCircle * MULTIPLIER).sqrMagnitude < 250; } sw.Stop(); UnityEngine.Debug.Log($"sqrMagnitude {sw.Elapsed.TotalMilliseconds}"); sw.Start(); for (int i = 0; i < c; i++) { var value = (UnityEngine.Random.insideUnitCircle * MULTIPLIER - UnityEngine.Random.insideUnitCircle * MULTIPLIER).magnitude < 250; } sw.Stop(); UnityEngine.Debug.Log($"magnitude {sw.Elapsed.TotalMilliseconds}"); sw.Start(); for (int i = 0; i < c; i++) { var value = Vector3.Distance((UnityEngine.Random.insideUnitCircle * MULTIPLIER - UnityEngine.Random.insideUnitCircle * MULTIPLIER), z) < 250; } sw.Stop(); UnityEngine.Debug.Log($"distance {sw.Elapsed.TotalMilliseconds}"); My times: sqrMagnitude 20,3163 magnitude 41,8683 distance 66,1406 Also these are debug performance measures, which are totally off. I highly suggest you do a RELEASE build and compare performance with that next time. Some functions do more DEBUG bogus than others and also some things get highly optimized by the compiler, while others don't. Please don't spread the rumor that Math.Sqrt is not a computational heavy thing for the computer. Also funny to note is that Math.Sqrt strangle for the same value is always faster than for other values. Also not significant but surely measureable. Don't know why honestly. FindObjectWithTag is searching the metadata of a gameobject and the FindObjectOfType need to search all components of all game objects and also do reflection on them since IIRC it works with interfaces. Using Linq is fine in Unity as long as you doing it right. With everything if you use it badly, it will do bad things. I love Linq in Unity and overall. Linq sometimes (depends what you are doing) will generate garbage. And the reason not to use Linq is for not generating unexpected garbage on mobile platforms, since GC is bad on there. I honestly use Linq all the time, but I do mainly PC Games. I'm really curious if caching the for loop makes a differernce on a optimized RELEASE build. The for loop should be faster on an array instead of a List. Because then the indexed data and accessing the index data[i] is way faster. Especially in your case. Try the for loop vs foreach with an array instead of a list and you should see the for loop be way superior. Reason behind this is that array data is cached on CPU cache and accessing it indexed is a single point read on memory. While a List is accessing a reference (the references are acutally cached in CPU) but the actual values behind the reference are not. So data[i] gets reference from CPU cache but has to resolve the reference which often will be a cache miss. And if you do that multiple times, it will be worse. This should get optimized by a RELEASE build. The foreach gets the reference value one time and will cache ich for you in your variable. That's why the for is slower when accessed multiple times without compiler optimization and with a List instead of an T[]. This being said the foreach is usually slower than a for (with an array and caching in mind), because it will instantiate an Enumerator. The JIT does alot behind the scene, but honestly test it with an array and you will see magic.