title: efficiently reading digit matrix without delimiter in python
body: <p>I have a file which contains digit [0-9] matrix without delimiter with shape (N, M). N would be ~50k, M would be ~50k.
  A small version of matrix file will be for example, <code>mat.txt</code></p>
  <pre><code>0012230012000
  0012230002300
  0012230004200
  </code></pre>
  <p>Right now I am using the following code, but I am not so satified with the speed.</p>
  <pre class="lang-py prettyprint-override"><code>def read_int_mat(path):
      &quot;&quot;&quot;
      Read a matrix of integer with [0-9], and with no delimiter.
      &quot;&quot;&quot;
      with open(path) as f:
          mat = np.array(
              [np.array([int(c) for c in line.strip()]) for line in f.readlines()],
              dtype=np.int8,
          )
      return mat
  </code></pre>
  <p>Edit:
  Here is a mini benchmark</p>
  <pre class="lang-py prettyprint-override"><code>import numpy as np
  def read_int_mat(path):
      &quot;&quot;&quot;
      Read a matrix of integer with [0-9], and with no delimiter.
      &quot;&quot;&quot;
      with open(path) as f:
          mat = np.array(
              [np.array([int(c) for c in line.strip()]) for line in f.readlines()],
              dtype=np.int8,
          )
      return mat
  
  %timeit read_int_mat(&quot;mat.txt&quot;)
  %timeit np.genfromtxt(&quot;mat.txt&quot;, delimiter=1, dtype=&quot;int8&quot;)
  
  print(read_int_mat(&quot;mat.txt&quot;))
  print(np.genfromtxt(&quot;mat.txt&quot;, delimiter=1, dtype=&quot;int8&quot;))
  </code></pre>
  <p>Outputs are:</p>
  <pre><code>61.6 µs ± 1.32 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
  327 µs ± 4.95 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
  [[0 0 1 2 2 3 0 0 1 2 0 0 0]
   [0 0 1 2 2 3 0 0 0 2 3 0 0]
   [0 0 1 2 2 3 0 0 0 4 2 0 0]]
  [[0 0 1 2 2 3 0 0 1 2 0 0 0]
   [0 0 1 2 2 3 0 0 0 2 3 0 0]
   [0 0 1 2 2 3 0 0 0 4 2 0 0]]
  </code></pre>
  <p>Is there any thing I could try to further speed it up. Would Cython help here? Many thanks in advance.</p>