Skip to content

Instantly share code, notes, and snippets.

@Ruszrok
Last active December 30, 2015 17:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ruszrok/7861319 to your computer and use it in GitHub Desktop.
Save Ruszrok/7861319 to your computer and use it in GitHub Desktop.
Microsoft.VisualBasic.FileIO.TextFieldParser vs String.Split
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.VisualBasic.FileIO;
using System.Diagnostics;
namespace ReaderComparer
{
class Program
{
static string[] ReadArrayFromFile(string fileName)
{
string[] result = null;
using (var reader = new StreamReader(fileName))
{
var size = Convert.ToInt32(reader.ReadLine());
result = reader.ReadLine()
.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
}
return result;
}
static string[] ReadArrayFromFileVb(string fileName)
{
var parser = new TextFieldParser(fileName);
parser.HasFieldsEnclosedInQuotes = true;
parser.SetDelimiters(" ");
string[] fields = null;
while (!parser.EndOfData)
{
fields = parser.ReadFields();
}
parser.Close();
return fields;
}
static void Main(string[] args)
{
var stopwatch = new Stopwatch();
stopwatch.Start();
ReadArrayFromFile(args[0]);
stopwatch.Stop();
Console.WriteLine(stopwatch.ElapsedMilliseconds);
var stopwatch1 = new Stopwatch();
stopwatch1.Start();
ReadArrayFromFileVb(args[0]);
stopwatch1.Stop();
Console.WriteLine(stopwatch1.ElapsedMilliseconds);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment