the_sea_jay
the_sea_jay
CC#
Created by the_sea_jay on 12/29/2022 in #help
❔ Best way to you Read lot of files?
I need something like a Filestream but where I can change the file thats read but each time I want to read another file I need to create a new FileStream which does extra allocation. See here:
public void T2()
{
var fs = new FileStream(@"./File.txt", FileMode.Open);
byte[] buffer = new byte[500];

for (int i = 0; i < fs.Length; i++)
{
buffer[i] = (byte)fs.ReadByte();
}

// do somthing with bytes in buffer where I get another file location

// need to do this cause we want another File but
fs = new FileStream(@"./AnotherFile.txt", FileMode.Open);

// Do someting with file

// need to do this cause we want another File but
fs = new FileStream(@"./YetAnotherFile.txt", FileMode.Open);

// and so on

fs.Close();

}
public void T2()
{
var fs = new FileStream(@"./File.txt", FileMode.Open);
byte[] buffer = new byte[500];

for (int i = 0; i < fs.Length; i++)
{
buffer[i] = (byte)fs.ReadByte();
}

// do somthing with bytes in buffer where I get another file location

// need to do this cause we want another File but
fs = new FileStream(@"./AnotherFile.txt", FileMode.Open);

// Do someting with file

// need to do this cause we want another File but
fs = new FileStream(@"./YetAnotherFile.txt", FileMode.Open);

// and so on

fs.Close();

}
Is there a better way which dont generate as much or even better, no(extra) garbage?
13 replies
CC#
Created by the_sea_jay on 12/16/2022 in #help
❔ Will I get a performance hit if I use the Enum type as a key value for a Dictionary?
See Code below:
public enum Tee1
{
None
}

public enum Tee2
{
None
}

class Program
{
static Dictionary<Enum, string> h = new Dictionary<Enum, string>();

static void Main(string[] args)
{
h.Add(Tee1.None, "Tee1 string");
h.Add(Tee2.None, "Tee2 string");

System.Console.WriteLine(h[Tee1.None]); // Output : Tee1 string
System.Console.WriteLine(h[Tee2.None]); // Output : Tee2 string
}
}
public enum Tee1
{
None
}

public enum Tee2
{
None
}

class Program
{
static Dictionary<Enum, string> h = new Dictionary<Enum, string>();

static void Main(string[] args)
{
h.Add(Tee1.None, "Tee1 string");
h.Add(Tee2.None, "Tee2 string");

System.Console.WriteLine(h[Tee1.None]); // Output : Tee1 string
System.Console.WriteLine(h[Tee2.None]); // Output : Tee2 string
}
}
Does this also create a hashmap? Or does the compiler do something unexpected here which costs performance?
32 replies