title: Sax parsing in GoLang - Slow speed
body: <p>Given the following code in C# / .NET Core 2.1</p>
  
  <pre><code>class Program
  {
      static void Main(string[] args)
      {
          var totalAmountSeconds = 0;
  
          using (var reader = XmlReader.Create(@"C:\Users\kevin\Desktop\temp\XML.xml"))
          {
              var stopWatch = new Stopwatch();
              var elementCounter = 0;
  
              stopWatch.Start();
  
              foreach (var element in XmlReaderUtils.EnumerateAxis(reader, new[] { "Node" }))
              {
                  elementCounter += 1;
              }
  
              stopWatch.Stop();
              totalAmountSeconds += stopWatch.Elapsed.Seconds;
  
              Console.WriteLine("Total '&lt;Node /&gt;' elements: " + elementCounter);
          }
  
          using (var reader = XmlReader.Create(@"XML.xml"))
          {
              var stopWatch = new Stopwatch();
              var elementCounter = 0;
  
              stopWatch.Start();
  
              foreach (var element in XmlReaderUtils.EnumerateAxis(reader, new[] { "ArticleGroup" }))
              {
                  elementCounter += 1;
              }
  
              stopWatch.Stop();
              totalAmountSeconds += stopWatch.Elapsed.Seconds;
  
              Console.WriteLine("Total '&lt;ArticleGroup /&gt;' elements: " + elementCounter);
          }
  
          Console.WriteLine("Total amount of required seconds: " + totalAmountSeconds);
      }
  }
  
  public static class XmlReaderUtils
  {
      public static IEnumerable&lt;XElement&gt; EnumerateAxis(XmlReader reader,
          string[] elements)
      {
          reader.MoveToContent();
  
          while (reader.Read())
          {
              switch (reader.NodeType)
              {
                  case XmlNodeType.Element:
                      if (!elements.Contains(reader.Name)) continue;
  
                      yield return XNode.ReadFrom(reader) as XElement;
  
                      break;
              }
          }
      }
  }
  </code></pre>
  
  <p>I do believe that an exact copy of this code in GoLang should be the following:</p>
  
  <pre><code>package main
  
  import (
      "encoding/xml"
      "fmt"
      "os"
      "time"
  )
  
  func main() {
      start := time.Now()
  
      xmlFile, _ := os.Open("XML.xml")
      defer xmlFile.Close()
  
      decoder := xml.NewDecoder(xmlFile)
  
      amountOfElements := 0
      for {
          t, _ := decoder.Token()
          if t == nil {
              break
          }
  
          // Inspect the type of the token just read.
          switch se := t.(type) {
          case xml.StartElement:
              if se.Name.Local == "Node" {
                  amountOfElements++
              }
          }
      }
  
      fmt.Printf("Total '&lt;Node /&gt;' elements: %v\n", amountOfElements)
  
      xmlFile, _ = os.Open("XML.xml")
      defer xmlFile.Close()
  
      decoder = xml.NewDecoder(xmlFile)
  
      amountOfElements = 0
      for {
          t, _ := decoder.Token()
          if t == nil {
              break
          }
  
          // Inspect the type of the token just read.
          switch se := t.(type) {
          case xml.StartElement:
              if se.Name.Local == "ArticleGroup" {
                  amountOfElements++
              }
          }
      }
  
      fmt.Printf("Total '&lt;ArticleGroup /&gt;' elements: %v\n", amountOfElements)
  
      fmt.Printf("Total elapsed time: %v", time.Since(start))
  }
  </code></pre>
  
  <p>The code execution in .NET takes 1 minutes, while in GoLang, it takes 2 minutes.
  The .NET version is built using Release configuration, go GoLang a <code>go build</code> is executed.</p>
  
  <p>I expected GoLang to be faster but indeed it turned out to be slower?
  Is this normal or am I missing something?</p>