{"id":128,"date":"2022-11-21T21:27:02","date_gmt":"2022-11-21T21:27:02","guid":{"rendered":"http:\/\/javigomez.org\/?p=128"},"modified":"2025-09-29T20:34:30","modified_gmt":"2025-09-29T20:34:30","slug":"medir-el-tiempo-transcurrido-en-java","status":"publish","type":"post","link":"https:\/\/javigomez.org\/index.php\/2022\/11\/21\/medir-el-tiempo-transcurrido-en-java\/","title":{"rendered":"Medir el tiempo transcurrido en Java"},"content":{"rendered":"\n<p>La clase System proporciona el m\u00e9todo nanoTime() para medir el tiempo que pasa en nanosegundos (1\/1.000.000.000 segundos) o tambi\u00e9n se puede utilizar el m\u00e9todo curretTimeMillis() que devuelve la diferencia entre la hora actual y la medianoche del 1\/1\/1970.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class timer {\n\n\tpublic static void main(String&#91;] args) {\n\t\t\n\t\t<strong>long inicio = System.currentTimeMillis();<\/strong>\n\t\t \n\t\t\/\/se hace un retardo con un bucle\n\t\t\/\/se puede hacer TimeUnet.SECONDS.sleep(5) para tardar 5 segundos\n\t\t\/\/import java.util.concurrent.TimeUnit;\n\t\t\/\/en este caso es necesario cambiar main\n\t\t\/\/public static void main(String&#91;] args) throws InterruptedException\n\t\tfor (long i=0;i&lt;500000;i++) {\n\t\t\tfor (long j=0;j&lt;5000;j++) {\n\t\t\t}\n\t\t}\n  \n        <strong>long fin = System.currentTimeMillis();<\/strong>\n\n        long transcurrido = fin - inicio;\n \n        System.out.println(\"Tiempo transcurrido en milisegundos: \" + transcurrido);\n\n      \/\/Ahora con nanosegundos\n        \n        <strong>inicio=System.nanoTime();<\/strong>\n\t\tfor (long i=0;i&lt;500000;i++) {\n\t\t\tfor (long j=0;j&lt;5000;j++) {\n\t\t\t}\n\t\t}\n\t\t<strong>fin=System.nanoTime();<\/strong>\n\t\ttranscurrido=fin-inicio;\n        System.out.println(\"Tiempo transcurrido en nanosegundos: \" + transcurrido + \" en milisegundos: \" + transcurrido\/1000000);\n\t}\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>La clase System proporciona el m\u00e9todo nanoTime() para medir el tiempo que pasa en nanosegundos (1\/1.000.000.000 segundos) o tambi\u00e9n se puede utilizar el m\u00e9todo curretTimeMillis() &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[107,203,241,323],"class_list":["post-128","post","type-post","status-publish","format-standard","hentry","category-java","tag-currenttimemillis","tag-java","tag-nanotime","tag-temporizador"],"_links":{"self":[{"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/posts\/128","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/comments?post=128"}],"version-history":[{"count":1,"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/posts\/128\/revisions"}],"predecessor-version":[{"id":983,"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/posts\/128\/revisions\/983"}],"wp:attachment":[{"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/media?parent=128"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/categories?post=128"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/tags?post=128"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}