Note that there are some explanatory texts on larger screens.

plurals
  1. PONeed suggestions on optimizing this code
    text
    copied!<p>Currently when I read a 15Mb file my application goes over a gig of memory. Notice that, at the end of the main code, I compare the data that was inserted in the database with the original array from the file. Any suggestions are welcome.</p> <p>Main code:</p> <pre><code>TestEntities entities = new TestEntities(); using (FileStream fileStream = new FileStream(fileName + ".exe", FileMode.Open, FileAccess.Read)) { byte[] bytes = new byte[fileStream.Length]; int numBytesToRead = (int) fileStream.Length; int numBytesRead = 0; while (numBytesToRead &gt; 0) { int n = fileStream.Read(bytes, numBytesRead, numBytesToRead); if (n == 0) break; numBytesRead += n; numBytesToRead -= n; } var query = bytes.Select((x, i) =&gt; new {Index = i, Value = x}) .GroupBy(x =&gt; x.Index/100) .Select(x =&gt; x.Select(v =&gt; v.Value).ToList()) .ToList(); foreach (List&lt;byte&gt; list in query) { Binary binary = new Binary(); binary.Name = fileName + ".exe"; binary.Value = list.ToArray(); entities.AddToBinaries(binary); } entities.SaveChanges(); List&lt;Binary&gt; fileString = entities.Binaries.Where(b =&gt; b.Name == fileName + ".exe").ToList(); Byte[] final = ExtractArray(fileString); if (Compare(bytes, final)) { /// Some notification that was ok } } </code></pre> <p>Compare Method:</p> <pre><code>public bool Compare(Byte[] array1,Byte[] array2) { bool isEqual = false; if (array1.Count() == array2.Count()) { for (int i = 0; i &lt; array1.Count(); i++) { isEqual = array1[i] == array2[i]; if (!isEqual) { break; } } } return isEqual; } </code></pre> <p>ExtractArray Method:</p> <pre><code>public Byte[] ExtractArray(List&lt;Binary&gt; binaries ) { List&lt;Byte&gt; finalArray = new List&lt;Byte&gt;(); foreach (Binary binary in binaries) { foreach (byte b in binary.Value) { finalArray.Add(b); } } return finalArray.ToArray(); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload