Release: Steuer System

12/22/2019 01:41 Irelia<3#1
NRunHandler.cs

Leider nur halb Fertig aber ich hoffe Ihr könnt damit was anfangen.
Code:
        //NosTale Steuern.
        public static void Steuern(ClientSession Session, NRunPacket packet)
        {
            if (Session.Character.Gold < 100000000)
            {
                int Prozent = 100;
                string ProzentString = Convert.ToString(Prozent);
                int Gold = 100000000;
                string GoldString = Convert.ToString(Gold);
                int Steuern = 5;
                string SteuernString = Convert.ToString(Steuern);
                Session.Character.Gold = Steuern / Gold * Prozent;
                int ergebnis = (Steuern / Gold) * Prozent;
                string ergebnisString = Convert.ToString(ergebnis);
                Session.SendPacket(UserInterfaceHelper.GenerateMsg(string.Format(Language.Instance.GetMessageFromergenisString("DU_MUSST_DIE_STEUERN_ZAHLEN", ergebnisString)), 0));
            }
            else
            {
                Session.Disconnect();
            }
        }
Language.cs
Code:
        public string GetMessageFromergenisString(string v, string ergebnisString)
        {
            return _language.GetOrAdd(ergebnisString, name =>
            {
                string value = _manager?.GetString(name, _resourceCulture);

                if (string.IsNullOrEmpty(value))
                {
                    _streamWriter?.WriteLine(name);
                    return "none";
                }

                return value;
            });
        }
12/22/2019 09:40 erixor#2
Why would you add anything to the language file ? Why would you code in german ? Why do you disconnect the session in a nrun handler ?

Also, instead of Convert.ToString, you could just use templated strings like $"{var}", which is slightly more efficient.

Also, I'm not sure that setting the character's gold this way is good, shouldn't it be a -= or a += ? (I can't understand german, so idk what you're trying to do here).

Also, you should generate the gold packet.
12/22/2019 11:09 Cryless~#3
Quote:
Originally Posted by erixor View Post
Why would you add anything to the language file ? Why would you code in german ? Why do you disconnect the session in a nrun handler ?

Also, instead of Convert.ToString, you could just use templated strings like $"{var}", which is slightly more efficient.

Also, I'm not sure that setting the character's gold this way is good, shouldn't it be a -= or a += ? (I can't understand german, so idk what you're trying to do here).

Also, you should generate the gold packet.
You should prefer it because it is cleaner but it is far from being more efficient. ;)

Code:
using System;
using System.Diagnostics;

namespace Foo
{
    class Program
    {
        static void Main(string[] args)
        {
            Stopwatch stopWatch = new Stopwatch();

            string bar;

            stopWatch.Start();

            for (int i = 0; i < 100000; i++)
            {
                bar = $"{i}";
            }

            stopWatch.Stop();

            Console.WriteLine($"string interpolation = {stopWatch.ElapsedMilliseconds} ms");

            stopWatch.Reset();

            stopWatch.Start();

            for (int i = 0; i < 100000; i++)
            {
                bar = i.ToString();
            }

            stopWatch.Stop();

            Console.WriteLine($"ToString = {stopWatch.ElapsedMilliseconds} ms");
        }
    }
}
Output:

Code:
% dotnet run
string interpolation = 10 ms
ToString = 4 ms
% dotnet run
string interpolation = 10 ms
ToString = 3 ms
% dotnet run
string interpolation = 10 ms
ToString = 3 ms
As you can see, it is 2-3 times slower than .ToString()! I suggest you testing next time.
12/22/2019 14:12 Blowa#4
Quote:
Originally Posted by Cryless~ View Post
You should prefer it because it is cleaner but it is far from being more efficient. ;)

Code:
using System;
using System.Diagnostics;

namespace Foo
{
    class Program
    {
        static void Main(string[] args)
        {
            Stopwatch stopWatch = new Stopwatch();

            string bar;

            stopWatch.Start();

            for (int i = 0; i < 100000; i++)
            {
                bar = $"{i}";
            }

            stopWatch.Stop();

            Console.WriteLine($"string interpolation = {stopWatch.ElapsedMilliseconds} ms");

            stopWatch.Reset();

            stopWatch.Start();

            for (int i = 0; i < 100000; i++)
            {
                bar = i.ToString();
            }

            stopWatch.Stop();

            Console.WriteLine($"ToString = {stopWatch.ElapsedMilliseconds} ms");
        }
    }
}
Output:

Code:
% dotnet run
string interpolation = 10 ms
ToString = 4 ms
% dotnet run
string interpolation = 10 ms
ToString = 3 ms
% dotnet run
string interpolation = 10 ms
ToString = 3 ms
As you can see, it is 2-3 times slower than .ToString()! I suggest you testing next time.
No one here is looking for micro optimizations of this kind, also Convert.ToString != object.ToString (if you test something, do it well)

String interpolation basically replace the given interpolated string with a string.Format, which uses a StringBuilder to finally construct the string so yeah, it's slower than doing a "single" call.

No sensed people would like to add this kind of micro optimization over losing maintainability on this kind of software/business logic
Especially when there are 3450435430 things that makes performances shitty on OpenNos and that a simple context switch is longer than the overhead brought by string interpolation
12/22/2019 14:31 Cryless~#5
Quote:
Originally Posted by val77 View Post
No one here is looking for micro optimizations of this kind, also Convert.ToString != object.ToString (if you test something, do it well)

String interpolation basically replace the given interpolated string with a string.Format, which uses a StringBuilder to finally construct the string so yeah, it's slower than doing a "single" call.
It depends on the needs you have, in some contexts even 1ms can become critical but it is also true that in that case you would most likely use different tools.

I never said that I care, indeed I said that you should prefer string interpolation because it is cleaner. But if you want to be arrogant, at least you could avoid spreading misinformation. It is not true that they are more efficient as shown.

That is all.

I have not tested Convert.ToString but most likely it does not change much. I will post the results in a few minutes to clarify.

I would also like to point out that using string interpolation or Convert.ToString makes no difference in this context. There is no concatenation so it does not complicate reading.
12/22/2019 14:42 erixor#6
Quote:
Originally Posted by Cryless~ View Post
You should prefer it because it is cleaner but it is far from being more efficient. ;)

[...]

As you can see, it is 2-3 times slower than .ToString()! I suggest you testing next time.
My bad then I messed up between string interpolation and string concatenation, which is the faster one, but it's still cleaner to use interpolation anyways... But meh, that doesn't really matter, does it ?
12/22/2019 14:43 Blowa#7
Quote:
Originally Posted by Cryless~ View Post
It depends on the needs you have, in some contexts even 1ms can become critical but it is also true that in that case you would most likely use different tools.

I never said that I care, indeed I said that you should prefer string interpolation because it is cleaner. But if you want to be arrogant, at least you could avoid spreading misinformation. It is not true that they are more efficient as shown.

That is all.

I have not tested Convert.ToString but most likely it does not change much. I will post the results in a few minutes to clarify.
Actually, you are talking about nanosecond optimization, if this kind of optimization is a need on your software, you should better not take C# but Go, Rust or C++ that are statically compiled (no JIT nor runtime interpreter VM shit) and severely optimized by their compilers (-o3...).

This is about some business logic code, which, in case of OpenNos, does not require nanosecond optimizations at all.

Convert.ToString will, probably in most common cases, be optimized by compilers, but it will, in a "normal non optimized" manner, call some runtime things to be able to use the right type, which have an overhead (probably a simple cast in most case).
12/22/2019 14:55 Cryless~#8
Quote:
Originally Posted by val77 View Post
Actually, you are talking about nanosecond optimization, if this kind of optimization is a need on your software, you should better not take C# but Go, Rust or C++ that are statically compiled (no JIT nor runtime interpreter VM shit) and severely optimized by their compilers (-o3...).

This is about some business logic code, which, in case of OpenNos, does not require nanosecond optimizations at all.

Convert.ToString will, probably in most common cases, be optimized by compilers, but it will, in a "normal non optimized" manner, call some runtime things to be able to use the right type, which have an overhead (probably a simple cast in most case).
You are completely right but it is not the point for which I intervened. I just wanted to clarify that string interpolations are not more efficient than object.ToString() nor Convert.ToString() in reference to @[Only registered and activated users can see links. Click Here To Register...]'s answer.

Some people claim that string interpolations use not only string.Format but also string.Concat, however, I do not know if it is true and it does not matter to me.

For those who are curious:

Code:
% dotnet run
string interpolation = 9 ms
Convert.ToString = 5 ms
% dotnet run
string interpolation = 9 ms
Convert.ToString = 4 ms
% dotnet run
string interpolation = 9 ms
Convert.ToString = 4 ms

The code was run on an Intel Core i9-8950HK.