title: How can I extract the numeric value from the time difference generated by Sys.time() in R?
body: <pre class="lang-r prettyprint-override"><code>t1 &lt;- Sys.time()
  mean((rnorm(10000))^2)
  t2 &lt;- Sys.time() 
  print(t2-t1)
  print(&quot; &quot;)
  
  t1 &lt;- Sys.time()
  mean((rnorm(10000))^2)
  t2 &lt;- Sys.time() 
  print(difftime(t2, t1, units = &quot;secs&quot;)[[1]])
  </code></pre>
  <p>I want to compare the time efficiency of a few algorithms for computing the same target, so I tried the two ways above to extract the time difference computed by <code>Sys.time()</code>. However, neither gives a clear numeric.</p>
  <pre><code>[1] 0.9998752
  Time difference of 0.03889418 secs
  [1] &quot; &quot;
  [1] 0.9832738
  [1] 0.05183697
  </code></pre>
  <p>I also tried <code>proc.time()</code>. It would be great to extract the 3 numeric values into a vector, but none of <code>as.numeric(t)</code>, <code>t[0]</code>, <code>t['user']</code>, and <code>t[['user']]</code> works. Those are some relevant solutions I found online. How can I get one (or three, either is fine) neat figure from the timing result?</p>
  <pre><code>t1 &lt;- proc.time()
  mean((rnorm(10000))^2)
  t2 &lt;- proc.time() 
  t &lt;- t2 - t1
  print(&quot; &quot;)
  print(t)
  
  
  [1] &quot; &quot;
     user  system elapsed 
     0.00    0.02    0.17 
  </code></pre>
  <hr />
  <p><strong>Is there an equivalent way in R to do what the code below does in Python?</strong></p>
  <pre class="lang-py prettyprint-override"><code>import numpy as np
  from time import process_time
  
  t = process_time()
  np.mean(np.random.normal(loc=0,scale=1,size=10000))
  t = process_time() - t
  print(t)
  </code></pre>