Why is using rawData slower than direct file access
<p>I forgot my password of a .pfx x509 certificate, so I decided I wanted to try to bruteforce it. So I went to test and wrote the following code:</p>

<pre><code>        for (var i = 0; i &lt; 1000; i++)
        {
            try
            {
                var cert = new X509Certificate2("D:/file.pfx", i.ToString(), X509KeyStorageFlags.MachineKeySet);
                Console.WriteLine("Found it: " + i);
            }catch
            {

            }
        }
</code></pre>

<p>I know it's far from optimal, but that's not what my question is about. It was a bit slow, so I thought, I can use the rawData overload instead of giving it the pathname. So I did the following code:</p>

<pre><code>        for (var i = 0; i &lt; 1000; i++)
        {
            try
            {
                cert = new X509Certificate2(buffer, i.ToString(), X509KeyStorageFlags.MachineKeySet);
                Console.WriteLine("Found it: " + i);
            }catch
            {}
        }
</code></pre>

<p>Where <code>buffer</code> is a <code>byte[]</code>; Outside of the loop I just loaded it in  with the raw data from the file with a <code>BinaryReader</code>. </p>

<p>Only this code is like 3 times slower. Why is that?</p>

<p><strong>Edit</strong>: I've ran this code like a 100 times now, so it's not just a random coincidence. </p>
