Closed arekbulski closed 6 years ago
You couldn't find the results since they weren't there, for some reason it isn't executing now.
I'll get it figured out later.
I reverted one change, mainly to keep it ieasier to understand. The VesselInFlight,, I have it checking the same values that the JumpPossible is checking, hopefully makes it easier to follow for someone who may not be familiar.
The test isn't running because you are getting an exception:
InvalidCastException: Cannot cast from source type to destination type.
at System.Linq.Enumerable+1[System.Double].MoveNext () [0x00000] in <filename unknown>:0 at System.Collections.Generic.List
1[System.Double].AddEnumerable (IEnumerable1 enumerable) [0x00000] in <filename unknown>:0 at System.Collections.Generic.List
1[System.Double]..ctor (IEnumerable1 collection) [0x00000] in <filename unknown>:0 at System.Linq.Enumerable.ToList[Double] (IEnumerable
1 source) [0x00000] in
Regarding your code, this statement doesn't work:
static List
data = new List<double>(); for (double i = 0; i < 1000000; i++) data.Add(i);
Regarding the timing results, hereis the results, and they are significant:
Beginning timing tests Timing results: Operator + Time: 551 ms/1_000_000 calls , Memory: 418414592/430108672/418459648 Timing results: String.Format Time: 995 ms/1_000_000 calls , Memory: 418455552/564322304/418467840 Timing results: StringBuilder Time: 653 ms/1_000_000 calls , Memory: 418459648/605696000/418476032 Timing results: StringBuilder 2 Time: 400 ms/1_000_000 calls , Memory: 418459648/590843904/418471936 Timing results: For loop Time: 3 ms/1_000_000 calls , Memory: 418459648/418459648/418459648 Timing results: Foreach loop Time: 5 ms/1_000_000 calls , Memory: 418459648/418459648/418459648 Completed timing tests
You'll notice that there is a StringBuilder 2, that is because you changed the test I was doing. It was a reasonable change, but I wanted to compare it to what I had. So, while TestStringBuilder is using ToString("N1"), TestStringBuilder2 is just using a simple Append(i). The tests show the following:
The + operator is nearly 1/2 the speed of the String.Format. StringBuilder2 (without the formatting of ToString) is actually the fasted, but that is more complicated to write, and less obvious as to what the final result is.
Regarding the for loops, the timing results show that the For is only 3/5 the time of the Foreach. Admittedly neither is a lot of time, but again, in a game like this, every bit (no pun intended) counts.
I've committed my changes, you can get them and examine the results for yourself
Regarding your valid comment about the time of Delegates, I don't think it's relevant since all the tests are doing the same thing. However, I'm adding a timing test which will test the delegate time only.
The timing result from the delegate timer is:
Timing results: Delegate Time: 0 ms/1_000_000 calls , Memory: 417718272/417718272/417718272
This is also committed
One more thing before I go to sleep:
A single mod, by itself, won't cause problems if it's wasting a few milliseconds here and there. However, the more mods that are added, the more milliseconds can be wasted, and soon you end up with a slow game. I know this personally since I play with more than 220 mods in my career game
Just saw this comment of yours:
where I pointed out that .NET generational GC can easily handle temporary instances, making guys "improvement" meaningless, he countered with the fact that Unity runs on Mono, oooold mark and sweep GC. I think you get the point.
The point he made, and which you seem to be ignoring, is that KSP is written in c# AND runs with Unity, so we have to deal with whatever version of .Net/Mono is with the game
Yeah, I have to admit, the evangelism of mine blinded me to the harsh shortcomings on KSP underlying framework. You see, I am a fan of "clean code" and very very often, I sacrifice these kind of speed issues outright, just for readability. Did I mention I prefer Python? (wink wink). In the world I live in, as imaginary as those newtons, speed drop of less than 10% is simply irrelevant.
I play with ~85 mods in KSP, its so mighty slow that I already reinstalled the game and dropped some of the mods, even the more important ones. So I understand your need, need for speed, and I accept that in these conditions that drop of water is worth saving. A bit of water, pun.
Test methodology. You should be subtracting the results, not dividing them. If measurements are [2s, 1.9s] then its 100ms difference that matters, not the 0.95 ratio. If its [0.001 0.002] then its 1ms that I would neglect, its not DOUBLE AS FAST, NEED FOR SPEED!... to me, its half a Infinitesimal, or half a ~zero.
That static list should have been:
static List<double> xxx = Enumerable.Range(0,10).Select<int,double>(x => x).ToList();
It seems to me that KSP crew (Squad) should have worked on improving Unity or replacing Mono version to something recent (working with Unity crew perhaps), not rewriting for loops. Thats just a kind of optimisation that I consider just aweful. Its like rewriting the code from doubles to floats, because its faster. For loops in game run with what, <100 iterations, not a million. This is shaving nanoseconds. You dont shave your beard with a microscope, do you? And you dont do that for a reason, if they probably bothered to do profiling on Unity, they would find room for improvement somewhere else than loops, and much more to gain as well. This is like replecing a carpet in your 20ton bulldozer because its "lighter".
And code they execute is much more expensive than the loop itself. How much speed did they squeeze out of it withat foreach->for change? Do you know, I am curious? I would guess <1 FPS on decent computer?
"In the world I live in, as imaginary as those newtons, speed drop of less than 10% is simply irrelevant." Should have added that if code runs <1sec then speed efficiency doesnt matter to me anyway.
After thinking, I think we should keep String.Format just for sake of simplicity and close the topic. Reason is: most of the time the FTL windows are hidden, and then nothing gets computed anyway. So performance doesnt matter almost all the time, in game time.
But I did appreciate the conversation and methodology. :)
It seems to me that KSP crew (Squad) should have worked on improving Unity or replacing Mono version to something recent (working with Unity crew perhaps), not rewriting for loops. Thats just a kind of optimisation that I consider just aweful. Its like rewriting the code from doubles to floats, because its faster. For loops in game run with what, <100 iterations, not a million. This is shaving nanoseconds. You dont shave your beard with a microscope, do you? And you dont do that for a reason, if they probably bothered to do profiling on Unity, they would find room for improvement somewhere else than loops, and much more to gain as well. This is like replecing a carpet in your 20ton bulldozer because its "lighter".
And code they execute is much more expensive than the loop itself. How much speed did they squeeze out of it withat foreach->for change? Do you know, I am curious? I would guess <1 FPS on decent computer?
It was actually pretty significant, all told. Each loop in itself isn't a lot, but they all add up. Squad is writing a game, not writing a game engine. There are lots of companies using Unity, Squad is only one. They made a choice years ago to use Unity; both they and we have to live with that decision. 100 iterations, 30-60 times a second, for each loop. It adds up. Without knowing what they were doing during development, and especially when they were doing the massive cleanup and optimizations, making statements like "if they probably bothered to do profiling" is nonsensical. Maybe they did. I don't know, and neither do you.
Regarding the String.Format, I'll see how it goes in my testing. I'll be testing in a very heavily modded game, and will see how bad the performance impact it.
I've also been replacing the "foreach" with "for (int ..." in those places where it makes sense. I gather you are done with your changes, so once I finish my testing, I'll release it
Thank you for your work. We obviously have different viewpoints, and to a certain extent, won't be able to convince the other. I'm approaching all this with more than 40 years of coding experience. What I've learned over the years is that languages aren't important, OS's aren't important, etc. What's most important is to choose the right tool for the job at hand, and, to use those tools which have been chosen to their best advantage. For anything other than a performance-based game, I'd be right up there with you in regards to readability, etc.
Oh, I forgot to ask: How do you want me to mention your contribution? Specifically, github user or a forum user?
Practicality over principles, thats another famous one from programming. I think we both agree on that one, but not on how much performance is worth how much effort in coding. Funny, now that I think of it, it reminds me of law of equivalent exchange, from Fullmetal Alchemist anime.
I prefer github credentials, Agrock is actually not my nickname, its just temporary account that web browser generated.
Yeah, I have I think ~15 years experience in that. Stopped using this "having x years exp" in chats because, actually I came to a conclusion that this is meaningless argument. Argument value is not dependant on who makes it, and so I stopped using the phrase "in my professional opinion" too. I would have some reservations to say that arguments exist in void (are valid even without any humans), since coding effort is one of primary metrics and its literally human-measure. But this is so filosofical that eh nevermind.
I enojyed working on your mod,
Just released it, take a look to make sure I didn't make any mistakes with some final edits.
So you kept TweakScale patch too huh? :smile:
I had to resubmit (and added spaces before units, as requested).
I also redone speed tests but cannot find results in my log. Could you run this code and paste measurements? And please look at it too.