Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

C# Optimizations #12

Open
jarnmo opened this issue Oct 24, 2022 · 3 comments
Open

C# Optimizations #12

jarnmo opened this issue Oct 24, 2022 · 3 comments

Comments

@jarnmo
Copy link

jarnmo commented Oct 24, 2022

Hey! Always fun to see these comparisons :)

Couple tips for improving the .net version performance:

You're using structs as readonly so change struct to readonly struct. This helps the compiler do some additional optimizations.

You can improve the parsing performance and memory consumption probably by a lot by reading the file to a single string and then splitting it into (ReadOnly)Spans as much as possible rather than creating lots and lots of small strings.

You can also enable rigorous null safety by adding <Nullable>enable</Nullable> to your project file. Or by other means described here: https://learn.microsoft.com/en-us/dotnet/csharp/nullable-references#nullable-contexts

@jarnmo
Copy link
Author

jarnmo commented Oct 24, 2022

Also for convenience you could use record structs and LINQ

So

public struct StopTimeResponse
{
  public string stop_id { get; }
  public string arrival_time { get; }
  public string departure_time { get; }
  public StopTimeResponse(string stopID, string arrival, string departure)
  {
    stop_id = stopID;
    arrival_time = arrival;
    departure_time = departure;
  }
}

into

public readonly record struct StopTimeResponse (string stopID, string arrival, string departure);

Also, in C# it's common to use PascalCase for public properties and methods.

LINQ: https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/linq/query-syntax-and-method-syntax-in-linq

@theolivenbaum
Copy link

Pushed some changes on #15 that should help here!

@Andoreatta
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants