[{"data":1,"prerenderedAt":1833},["ShallowReactive",2],{"doc-\u002Fstandard-library\u002Ftime.time-function-explained":3},{"id":4,"title":5,"body":6,"description":1826,"extension":1827,"meta":1828,"navigation":91,"path":1829,"seo":1830,"stem":1831,"__hash__":1832},"content\u002Fstandard-library\u002Ftime.time-function-explained.md","time.time() Function Explained",{"type":7,"value":8,"toc":1792},"minimark",[9,18,24,36,46,59,64,205,210,213,228,235,240,263,266,324,327,358,361,365,374,415,418,435,443,446,472,478,482,485,493,496,504,511,520,574,587,591,597,611,614,665,668,767,780,787,790,793,808,811,968,970,1009,1012,1019,1023,1028,1046,1049,1068,1072,1077,1085,1091,1122,1125,1164,1168,1171,1200,1205,1210,1219,1222,1234,1236,1311,1321,1324,1348,1351,1379,1383,1389,1392,1477,1480,1555,1561,1567,1570,1586,1589,1609,1612,1630,1633,1651,1659,1663,1670,1673,1680,1683,1690,1693,1700,1706,1740,1744,1777,1788],[10,11,13,17],"h1",{"id":12},"timetime-function-explained",[14,15,16],"code",{},"time.time()"," Function Explained",[19,20,21,23],"p",{},[14,22,16],{}," returns the current time as a number.",[19,25,26,27,31,32,35],{},"In Python, this number is the current ",[28,29,30],"strong",{},"Unix timestamp",": the number of seconds since ",[28,33,34],{},"January 1, 1970 UTC",". Beginners often use it for two simple tasks:",[37,38,39,43],"ul",{},[40,41,42],"li",{},"getting the current timestamp",[40,44,45],{},"measuring how long some code takes to run",[19,47,48,49,52,53,58],{},"If you want a readable date like ",[14,50,51],{},"2025-04-22 10:30:00",", use the ",[54,55,57],"a",{"href":56},"\u002Fstandard-library\u002Fpython-datetime-module-overview","Python datetime module overview"," instead.",[60,61,63],"h2",{"id":62},"quick-example","Quick example",[65,66,71],"pre",{"className":67,"code":68,"language":69,"meta":70,"style":70},"language-python shiki shiki-themes material-theme-lighter github-light github-dark","import time\n\ncurrent_timestamp = time.time()\nprint(current_timestamp)\n\nstart = time.time()\ntime.sleep(1)\nend = time.time()\nprint(end - start)\n","python","",[14,72,73,86,93,117,133,138,154,172,188],{"__ignoreMap":70},[74,75,78,82],"span",{"class":76,"line":77},"line",1,[74,79,81],{"class":80},"sVHd0","import",[74,83,85],{"class":84},"su5hD"," time\n",[74,87,89],{"class":76,"line":88},2,[74,90,92],{"emptyLinePlaceholder":91},true,"\n",[74,94,96,99,103,106,110,114],{"class":76,"line":95},3,[74,97,98],{"class":84},"current_timestamp ",[74,100,102],{"class":101},"smGrS","=",[74,104,105],{"class":84}," time",[74,107,109],{"class":108},"sP7_E",".",[74,111,113],{"class":112},"slqww","time",[74,115,116],{"class":108},"()\n",[74,118,120,124,127,130],{"class":76,"line":119},4,[74,121,123],{"class":122},"sptTA","print",[74,125,126],{"class":108},"(",[74,128,129],{"class":112},"current_timestamp",[74,131,132],{"class":108},")\n",[74,134,136],{"class":76,"line":135},5,[74,137,92],{"emptyLinePlaceholder":91},[74,139,141,144,146,148,150,152],{"class":76,"line":140},6,[74,142,143],{"class":84},"start ",[74,145,102],{"class":101},[74,147,105],{"class":84},[74,149,109],{"class":108},[74,151,113],{"class":112},[74,153,116],{"class":108},[74,155,157,159,161,164,166,170],{"class":76,"line":156},7,[74,158,113],{"class":84},[74,160,109],{"class":108},[74,162,163],{"class":112},"sleep",[74,165,126],{"class":108},[74,167,169],{"class":168},"srdBf","1",[74,171,132],{"class":108},[74,173,175,178,180,182,184,186],{"class":76,"line":174},8,[74,176,177],{"class":84},"end ",[74,179,102],{"class":101},[74,181,105],{"class":84},[74,183,109],{"class":108},[74,185,113],{"class":112},[74,187,116],{"class":108},[74,189,191,193,195,197,200,203],{"class":76,"line":190},9,[74,192,123],{"class":122},[74,194,126],{"class":108},[74,196,177],{"class":112},[74,198,199],{"class":101},"-",[74,201,202],{"class":112}," start",[74,204,132],{"class":108},[19,206,207,209],{},[14,208,16],{}," returns the current time as seconds since the Unix epoch. It is often used for simple timing.",[19,211,212],{},"Example output:",[65,214,216],{"className":67,"code":215,"language":69,"meta":70,"style":70},"1712345678.12345\n1.00012\n",[14,217,218,223],{"__ignoreMap":70},[74,219,220],{"class":76,"line":77},[74,221,222],{"class":168},"1712345678.12345\n",[74,224,225],{"class":76,"line":88},[74,226,227],{"class":168},"1.00012\n",[60,229,231,232,234],{"id":230},"what-timetime-does","What ",[14,233,16],{}," does",[19,236,237,239],{},[14,238,16],{},":",[37,241,242,248,251,260],{},[40,243,244,245],{},"returns the current time as a ",[14,246,247],{},"float",[40,249,250],{},"gives the number of seconds since January 1, 1970 UTC",[40,252,253,254,256,257],{},"returns what is often called a ",[28,255,30],{}," or ",[28,258,259],{},"epoch time",[40,261,262],{},"includes fractions of a second in the decimal part",[19,264,265],{},"A simple example:",[65,267,269],{"className":67,"code":268,"language":69,"meta":70,"style":70},"import time\n\ntimestamp = time.time()\nprint(timestamp)\nprint(type(timestamp))\n",[14,270,271,277,281,296,307],{"__ignoreMap":70},[74,272,273,275],{"class":76,"line":77},[74,274,81],{"class":80},[74,276,85],{"class":84},[74,278,279],{"class":76,"line":88},[74,280,92],{"emptyLinePlaceholder":91},[74,282,283,286,288,290,292,294],{"class":76,"line":95},[74,284,285],{"class":84},"timestamp ",[74,287,102],{"class":101},[74,289,105],{"class":84},[74,291,109],{"class":108},[74,293,113],{"class":112},[74,295,116],{"class":108},[74,297,298,300,302,305],{"class":76,"line":119},[74,299,123],{"class":122},[74,301,126],{"class":108},[74,303,304],{"class":112},"timestamp",[74,306,132],{"class":108},[74,308,309,311,313,317,319,321],{"class":76,"line":135},[74,310,123],{"class":122},[74,312,126],{"class":108},[74,314,316],{"class":315},"sZMiF","type",[74,318,126],{"class":108},[74,320,304],{"class":112},[74,322,323],{"class":108},"))\n",[19,325,326],{},"Possible output:",[65,328,330],{"className":67,"code":329,"language":69,"meta":70,"style":70},"1712345678.12345\n\u003Cclass 'float'>\n",[14,331,332,336],{"__ignoreMap":70},[74,333,334],{"class":76,"line":77},[74,335,222],{"class":168},[74,337,338,341,345,349,352,355],{"class":76,"line":88},[74,339,340],{"class":101},"\u003C",[74,342,344],{"class":343},"sbsja","class",[74,346,348],{"class":347},"sjJ54"," '",[74,350,247],{"class":351},"s_sjI",[74,353,354],{"class":347},"'",[74,356,357],{"class":101},">\n",[19,359,360],{},"This value is useful for storing or comparing times, but it is not easy for humans to read.",[60,362,364],{"id":363},"basic-syntax","Basic syntax",[19,366,367,368,370,371,373],{},"First, import the ",[14,369,113],{}," module. Then call ",[14,372,16],{}," with no arguments.",[65,375,377],{"className":67,"code":376,"language":69,"meta":70,"style":70},"import time\n\ncurrent_time = time.time()\nprint(current_time)\n",[14,378,379,385,389,404],{"__ignoreMap":70},[74,380,381,383],{"class":76,"line":77},[74,382,81],{"class":80},[74,384,85],{"class":84},[74,386,387],{"class":76,"line":88},[74,388,92],{"emptyLinePlaceholder":91},[74,390,391,394,396,398,400,402],{"class":76,"line":95},[74,392,393],{"class":84},"current_time ",[74,395,102],{"class":101},[74,397,105],{"class":84},[74,399,109],{"class":108},[74,401,113],{"class":112},[74,403,116],{"class":108},[74,405,406,408,410,413],{"class":76,"line":119},[74,407,123],{"class":122},[74,409,126],{"class":108},[74,411,412],{"class":112},"current_time",[74,414,132],{"class":108},[19,416,417],{},"Key points:",[37,419,420,427,432],{},[40,421,422,423,426],{},"you must ",[14,424,425],{},"import time"," first",[40,428,429,431],{},[14,430,16],{}," takes no arguments",[40,433,434],{},"store the result in a variable if you want to use it again later",[19,436,437,438,440,441,109],{},"If you used ",[14,439,425],{},", you must call the function as ",[14,442,16],{},[19,444,445],{},"This will fail:",[65,447,449],{"className":67,"code":448,"language":69,"meta":70,"style":70},"import time\n\nprint(time())\n",[14,450,451,457,461],{"__ignoreMap":70},[74,452,453,455],{"class":76,"line":77},[74,454,81],{"class":80},[74,456,85],{"class":84},[74,458,459],{"class":76,"line":88},[74,460,92],{"emptyLinePlaceholder":91},[74,462,463,465,467,469],{"class":76,"line":95},[74,464,123],{"class":122},[74,466,126],{"class":108},[74,468,113],{"class":112},[74,470,471],{"class":108},"())\n",[19,473,474,475,477],{},"Because ",[14,476,113],{}," is the module name, not the function name.",[60,479,481],{"id":480},"what-the-return-value-looks-like","What the return value looks like",[19,483,484],{},"The result is usually a large floating-point number, such as:",[65,486,487],{"className":67,"code":222,"language":69,"meta":70,"style":70},[14,488,489],{"__ignoreMap":70},[74,490,491],{"class":76,"line":77},[74,492,222],{"class":168},[19,494,495],{},"This means:",[37,497,498,501],{},[40,499,500],{},"the whole number part is the number of full seconds",[40,502,503],{},"the decimal part is the fraction of a second",[19,505,506,507,510],{},"It is ",[28,508,509],{},"not"," a formatted date string.",[19,512,513,514,239],{},"If you want only whole seconds, you can convert it with ",[54,515,517],{"href":516},"\u002Freference\u002Fpython-int-function-explained",[14,518,519],{},"int()",[65,521,523],{"className":67,"code":522,"language":69,"meta":70,"style":70},"import time\n\ntimestamp = time.time()\nprint(timestamp)\nprint(int(timestamp))\n",[14,524,525,531,535,549,559],{"__ignoreMap":70},[74,526,527,529],{"class":76,"line":77},[74,528,81],{"class":80},[74,530,85],{"class":84},[74,532,533],{"class":76,"line":88},[74,534,92],{"emptyLinePlaceholder":91},[74,536,537,539,541,543,545,547],{"class":76,"line":95},[74,538,285],{"class":84},[74,540,102],{"class":101},[74,542,105],{"class":84},[74,544,109],{"class":108},[74,546,113],{"class":112},[74,548,116],{"class":108},[74,550,551,553,555,557],{"class":76,"line":119},[74,552,123],{"class":122},[74,554,126],{"class":108},[74,556,304],{"class":112},[74,558,132],{"class":108},[74,560,561,563,565,568,570,572],{"class":76,"line":135},[74,562,123],{"class":122},[74,564,126],{"class":108},[74,566,567],{"class":315},"int",[74,569,126],{"class":108},[74,571,304],{"class":112},[74,573,323],{"class":108},[19,575,576,577,584,585,109],{},"If you want a readable date and time, look at ",[54,578,580,583],{"href":579},"\u002Fstandard-library\u002Fdatetime.now-explained",[14,581,582],{},"datetime.now()"," explained"," or the ",[54,586,57],{"href":56},[60,588,590],{"id":589},"common-use-cases","Common use cases",[19,592,593,594,596],{},"Beginners usually use ",[14,595,16],{}," for simple tasks like these:",[37,598,599,602,605,608],{},[40,600,601],{},"get the current timestamp",[40,603,604],{},"measure how long code takes to run",[40,606,607],{},"compare two times by subtraction",[40,609,610],{},"store simple time values in logs or files",[19,612,613],{},"Example: getting the current timestamp",[65,615,617],{"className":67,"code":616,"language":69,"meta":70,"style":70},"import time\n\ntimestamp = time.time()\nprint(\"Current timestamp:\", timestamp)\n",[14,618,619,625,629,643],{"__ignoreMap":70},[74,620,621,623],{"class":76,"line":77},[74,622,81],{"class":80},[74,624,85],{"class":84},[74,626,627],{"class":76,"line":88},[74,628,92],{"emptyLinePlaceholder":91},[74,630,631,633,635,637,639,641],{"class":76,"line":95},[74,632,285],{"class":84},[74,634,102],{"class":101},[74,636,105],{"class":84},[74,638,109],{"class":108},[74,640,113],{"class":112},[74,642,116],{"class":108},[74,644,645,647,649,652,655,657,660,663],{"class":76,"line":119},[74,646,123],{"class":122},[74,648,126],{"class":108},[74,650,651],{"class":347},"\"",[74,653,654],{"class":351},"Current timestamp:",[74,656,651],{"class":347},[74,658,659],{"class":108},",",[74,661,662],{"class":112}," timestamp",[74,664,132],{"class":108},[19,666,667],{},"Example: comparing two times",[65,669,671],{"className":67,"code":670,"language":69,"meta":70,"style":70},"import time\n\nfirst = time.time()\ntime.sleep(2)\nsecond = time.time()\n\ndifference = second - first\nprint(\"Seconds between times:\", difference)\n",[14,672,673,679,683,698,713,728,732,747],{"__ignoreMap":70},[74,674,675,677],{"class":76,"line":77},[74,676,81],{"class":80},[74,678,85],{"class":84},[74,680,681],{"class":76,"line":88},[74,682,92],{"emptyLinePlaceholder":91},[74,684,685,688,690,692,694,696],{"class":76,"line":95},[74,686,687],{"class":84},"first ",[74,689,102],{"class":101},[74,691,105],{"class":84},[74,693,109],{"class":108},[74,695,113],{"class":112},[74,697,116],{"class":108},[74,699,700,702,704,706,708,711],{"class":76,"line":119},[74,701,113],{"class":84},[74,703,109],{"class":108},[74,705,163],{"class":112},[74,707,126],{"class":108},[74,709,710],{"class":168},"2",[74,712,132],{"class":108},[74,714,715,718,720,722,724,726],{"class":76,"line":135},[74,716,717],{"class":84},"second ",[74,719,102],{"class":101},[74,721,105],{"class":84},[74,723,109],{"class":108},[74,725,113],{"class":112},[74,727,116],{"class":108},[74,729,730],{"class":76,"line":140},[74,731,92],{"emptyLinePlaceholder":91},[74,733,734,737,739,742,744],{"class":76,"line":156},[74,735,736],{"class":84},"difference ",[74,738,102],{"class":101},[74,740,741],{"class":84}," second ",[74,743,199],{"class":101},[74,745,746],{"class":84}," first\n",[74,748,749,751,753,755,758,760,762,765],{"class":76,"line":174},[74,750,123],{"class":122},[74,752,126],{"class":108},[74,754,651],{"class":347},[74,756,757],{"class":351},"Seconds between times:",[74,759,651],{"class":347},[74,761,659],{"class":108},[74,763,764],{"class":112}," difference",[74,766,132],{"class":108},[19,768,769,770,776,777,109],{},"In this example, ",[54,771,773],{"href":772},"\u002Fstandard-library\u002Ftime.sleep-function-explained",[14,774,775],{},"time.sleep()"," pauses the program for about 2 seconds, so the difference is about ",[14,778,779],{},"2.0",[60,781,783,784,786],{"id":782},"using-timetime-to-measure-elapsed-time","Using ",[14,785,16],{}," to measure elapsed time",[19,788,789],{},"This is one of the most common beginner uses.",[19,791,792],{},"The pattern is:",[794,795,796,799,802,805],"ol",{},[40,797,798],{},"save the start time",[40,800,801],{},"run your code",[40,803,804],{},"save the end time",[40,806,807],{},"subtract start from end",[19,809,810],{},"Example:",[65,812,814],{"className":67,"code":813,"language":69,"meta":70,"style":70},"import time\n\nstart = time.time()\n\nfor i in range(3):\n    print(\"Working...\")\n    time.sleep(0.5)\n\nend = time.time()\n\nelapsed = end - start\nprint(\"Elapsed time:\", elapsed, \"seconds\")\n",[14,815,816,822,826,840,844,866,882,898,902,916,921,937],{"__ignoreMap":70},[74,817,818,820],{"class":76,"line":77},[74,819,81],{"class":80},[74,821,85],{"class":84},[74,823,824],{"class":76,"line":88},[74,825,92],{"emptyLinePlaceholder":91},[74,827,828,830,832,834,836,838],{"class":76,"line":95},[74,829,143],{"class":84},[74,831,102],{"class":101},[74,833,105],{"class":84},[74,835,109],{"class":108},[74,837,113],{"class":112},[74,839,116],{"class":108},[74,841,842],{"class":76,"line":119},[74,843,92],{"emptyLinePlaceholder":91},[74,845,846,849,852,855,858,860,863],{"class":76,"line":135},[74,847,848],{"class":80},"for",[74,850,851],{"class":84}," i ",[74,853,854],{"class":80},"in",[74,856,857],{"class":122}," range",[74,859,126],{"class":108},[74,861,862],{"class":168},"3",[74,864,865],{"class":108},"):\n",[74,867,868,871,873,875,878,880],{"class":76,"line":140},[74,869,870],{"class":122},"    print",[74,872,126],{"class":108},[74,874,651],{"class":347},[74,876,877],{"class":351},"Working...",[74,879,651],{"class":347},[74,881,132],{"class":108},[74,883,884,887,889,891,893,896],{"class":76,"line":156},[74,885,886],{"class":84},"    time",[74,888,109],{"class":108},[74,890,163],{"class":112},[74,892,126],{"class":108},[74,894,895],{"class":168},"0.5",[74,897,132],{"class":108},[74,899,900],{"class":76,"line":174},[74,901,92],{"emptyLinePlaceholder":91},[74,903,904,906,908,910,912,914],{"class":76,"line":190},[74,905,177],{"class":84},[74,907,102],{"class":101},[74,909,105],{"class":84},[74,911,109],{"class":108},[74,913,113],{"class":112},[74,915,116],{"class":108},[74,917,919],{"class":76,"line":918},10,[74,920,92],{"emptyLinePlaceholder":91},[74,922,924,927,929,932,934],{"class":76,"line":923},11,[74,925,926],{"class":84},"elapsed ",[74,928,102],{"class":101},[74,930,931],{"class":84}," end ",[74,933,199],{"class":101},[74,935,936],{"class":84}," start\n",[74,938,940,942,944,946,949,951,953,956,958,961,964,966],{"class":76,"line":939},12,[74,941,123],{"class":122},[74,943,126],{"class":108},[74,945,651],{"class":347},[74,947,948],{"class":351},"Elapsed time:",[74,950,651],{"class":347},[74,952,659],{"class":108},[74,954,955],{"class":112}," elapsed",[74,957,659],{"class":108},[74,959,960],{"class":347}," \"",[74,962,963],{"class":351},"seconds",[74,965,651],{"class":347},[74,967,132],{"class":108},[19,969,326],{},[65,971,973],{"className":67,"code":972,"language":69,"meta":70,"style":70},"Working...\nWorking...\nWorking...\nElapsed time: 1.5008 seconds\n",[14,974,975,984,990,996],{"__ignoreMap":70},[74,976,977,980],{"class":76,"line":77},[74,978,979],{"class":84},"Working",[74,981,983],{"class":982},"s_hVV","...\n",[74,985,986,988],{"class":76,"line":88},[74,987,979],{"class":84},[74,989,983],{"class":982},[74,991,992,994],{"class":76,"line":95},[74,993,979],{"class":84},[74,995,983],{"class":982},[74,997,998,1001,1003,1006],{"class":76,"line":119},[74,999,1000],{"class":84},"Elapsed time",[74,1002,239],{"class":108},[74,1004,1005],{"class":168}," 1.5008",[74,1007,1008],{"class":84}," seconds\n",[19,1010,1011],{},"This works well for simple timing examples and quick checks.",[19,1013,1014,1015,109],{},"If you want to learn more about the module, see the ",[54,1016,1018],{"href":1017},"\u002Fstandard-library\u002Fpython-time-module-overview","Python time module overview",[60,1020,1022],{"id":1021},"limits-beginners-should-know","Limits beginners should know",[19,1024,1025,1027],{},[14,1026,16],{}," is useful, but it has some limits:",[37,1029,1030,1033,1036,1039],{},[40,1031,1032],{},"the result depends on your system clock",[40,1034,1035],{},"it is not the best tool for precise benchmarking",[40,1037,1038],{},"it does not give a human-readable date by itself",[40,1040,1041,1042],{},"it is not a replacement for delays like ",[54,1043,1044],{"href":772},[14,1045,775],{},[19,1047,1048],{},"So a good rule is:",[37,1050,1051,1057,1063],{},[40,1052,1053,1054,1056],{},"use ",[14,1055,16],{}," for timestamps and simple elapsed-time checks",[40,1058,1053,1059,1062],{},[14,1060,1061],{},"datetime"," for readable dates and times",[40,1064,1053,1065,1067],{},[14,1066,775],{}," when you want to pause your program",[60,1069,1071],{"id":1070},"common-mistakes","Common mistakes",[19,1073,1074,1075,109],{},"Here are some common problems beginners run into with ",[14,1076,16],{},[1078,1079,1081,1082,1084],"h3",{"id":1080},"forgetting-to-import-the-time-module","Forgetting to import the ",[14,1083,113],{}," module",[19,1086,1087,1088,1090],{},"This causes an error because Python does not know what ",[14,1089,113],{}," means yet.",[65,1092,1094],{"className":67,"code":1093,"language":69,"meta":70,"style":70},"current = time.time()\nprint(current)\n",[14,1095,1096,1111],{"__ignoreMap":70},[74,1097,1098,1101,1103,1105,1107,1109],{"class":76,"line":77},[74,1099,1100],{"class":84},"current ",[74,1102,102],{"class":101},[74,1104,105],{"class":84},[74,1106,109],{"class":108},[74,1108,113],{"class":112},[74,1110,116],{"class":108},[74,1112,1113,1115,1117,1120],{"class":76,"line":88},[74,1114,123],{"class":122},[74,1116,126],{"class":108},[74,1118,1119],{"class":112},"current",[74,1121,132],{"class":108},[19,1123,1124],{},"Fix:",[65,1126,1128],{"className":67,"code":1127,"language":69,"meta":70,"style":70},"import time\n\ncurrent = time.time()\nprint(current)\n",[14,1129,1130,1136,1140,1154],{"__ignoreMap":70},[74,1131,1132,1134],{"class":76,"line":77},[74,1133,81],{"class":80},[74,1135,85],{"class":84},[74,1137,1138],{"class":76,"line":88},[74,1139,92],{"emptyLinePlaceholder":91},[74,1141,1142,1144,1146,1148,1150,1152],{"class":76,"line":95},[74,1143,1100],{"class":84},[74,1145,102],{"class":101},[74,1147,105],{"class":84},[74,1149,109],{"class":108},[74,1151,113],{"class":112},[74,1153,116],{"class":108},[74,1155,1156,1158,1160,1162],{"class":76,"line":119},[74,1157,123],{"class":122},[74,1159,126],{"class":108},[74,1161,1119],{"class":112},[74,1163,132],{"class":108},[1078,1165,1167],{"id":1166},"expecting-a-formatted-date-string","Expecting a formatted date string",[19,1169,1170],{},"This:",[65,1172,1174],{"className":67,"code":1173,"language":69,"meta":70,"style":70},"import time\n\nprint(time.time())\n",[14,1175,1176,1182,1186],{"__ignoreMap":70},[74,1177,1178,1180],{"class":76,"line":77},[74,1179,81],{"class":80},[74,1181,85],{"class":84},[74,1183,1184],{"class":76,"line":88},[74,1185,92],{"emptyLinePlaceholder":91},[74,1187,1188,1190,1192,1194,1196,1198],{"class":76,"line":95},[74,1189,123],{"class":122},[74,1191,126],{"class":108},[74,1193,113],{"class":112},[74,1195,109],{"class":108},[74,1197,113],{"class":112},[74,1199,471],{"class":108},[19,1201,1202,1203,109],{},"returns a number, not something like ",[14,1204,51],{},[19,1206,1207,1208,109],{},"If you need a readable date, use the ",[54,1209,57],{"href":56},[1078,1211,1213,1214,1216,1217],{"id":1212},"confusing-timetime-with-timesleep","Confusing ",[14,1215,16],{}," with ",[14,1218,775],{},[19,1220,1221],{},"These functions do different jobs:",[37,1223,1224,1229],{},[40,1225,1226,1228],{},[14,1227,16],{}," gets the current timestamp",[40,1230,1231,1233],{},[14,1232,775],{}," pauses the program",[19,1235,810],{},[65,1237,1239],{"className":67,"code":1238,"language":69,"meta":70,"style":70},"import time\n\nprint(\"Before:\", time.time())\ntime.sleep(1)\nprint(\"After:\", time.time())\n",[14,1240,1241,1247,1251,1274,1288],{"__ignoreMap":70},[74,1242,1243,1245],{"class":76,"line":77},[74,1244,81],{"class":80},[74,1246,85],{"class":84},[74,1248,1249],{"class":76,"line":88},[74,1250,92],{"emptyLinePlaceholder":91},[74,1252,1253,1255,1257,1259,1262,1264,1266,1268,1270,1272],{"class":76,"line":95},[74,1254,123],{"class":122},[74,1256,126],{"class":108},[74,1258,651],{"class":347},[74,1260,1261],{"class":351},"Before:",[74,1263,651],{"class":347},[74,1265,659],{"class":108},[74,1267,105],{"class":112},[74,1269,109],{"class":108},[74,1271,113],{"class":112},[74,1273,471],{"class":108},[74,1275,1276,1278,1280,1282,1284,1286],{"class":76,"line":119},[74,1277,113],{"class":84},[74,1279,109],{"class":108},[74,1281,163],{"class":112},[74,1283,126],{"class":108},[74,1285,169],{"class":168},[74,1287,132],{"class":108},[74,1289,1290,1292,1294,1296,1299,1301,1303,1305,1307,1309],{"class":76,"line":135},[74,1291,123],{"class":122},[74,1293,126],{"class":108},[74,1295,651],{"class":347},[74,1297,1298],{"class":351},"After:",[74,1300,651],{"class":347},[74,1302,659],{"class":108},[74,1304,105],{"class":112},[74,1306,109],{"class":108},[74,1308,113],{"class":112},[74,1310,471],{"class":108},[1078,1312,1314,1315,1318,1319],{"id":1313},"calling-time-directly-after-import-time","Calling ",[14,1316,1317],{},"time()"," directly after ",[14,1320,425],{},[19,1322,1323],{},"This is wrong:",[65,1325,1326],{"className":67,"code":448,"language":69,"meta":70,"style":70},[14,1327,1328,1334,1338],{"__ignoreMap":70},[74,1329,1330,1332],{"class":76,"line":77},[74,1331,81],{"class":80},[74,1333,85],{"class":84},[74,1335,1336],{"class":76,"line":88},[74,1337,92],{"emptyLinePlaceholder":91},[74,1339,1340,1342,1344,1346],{"class":76,"line":95},[74,1341,123],{"class":122},[74,1343,126],{"class":108},[74,1345,113],{"class":112},[74,1347,471],{"class":108},[19,1349,1350],{},"This is correct:",[65,1352,1353],{"className":67,"code":1173,"language":69,"meta":70,"style":70},[14,1354,1355,1361,1365],{"__ignoreMap":70},[74,1356,1357,1359],{"class":76,"line":77},[74,1358,81],{"class":80},[74,1360,85],{"class":84},[74,1362,1363],{"class":76,"line":88},[74,1364,92],{"emptyLinePlaceholder":91},[74,1366,1367,1369,1371,1373,1375,1377],{"class":76,"line":95},[74,1368,123],{"class":122},[74,1370,126],{"class":108},[74,1372,113],{"class":112},[74,1374,109],{"class":108},[74,1376,113],{"class":112},[74,1378,471],{"class":108},[1078,1380,1382],{"id":1381},"rounding-too-early","Rounding too early",[19,1384,1385,1386,1388],{},"If you round or convert to ",[14,1387,519],{}," too soon, you may lose useful timing detail.",[19,1390,1391],{},"Less precise:",[65,1393,1395],{"className":67,"code":1394,"language":69,"meta":70,"style":70},"import time\n\nstart = int(time.time())\ntime.sleep(1.2)\nend = int(time.time())\n\nprint(end - start)\n",[14,1396,1397,1403,1407,1426,1441,1459,1463],{"__ignoreMap":70},[74,1398,1399,1401],{"class":76,"line":77},[74,1400,81],{"class":80},[74,1402,85],{"class":84},[74,1404,1405],{"class":76,"line":88},[74,1406,92],{"emptyLinePlaceholder":91},[74,1408,1409,1411,1413,1416,1418,1420,1422,1424],{"class":76,"line":95},[74,1410,143],{"class":84},[74,1412,102],{"class":101},[74,1414,1415],{"class":315}," int",[74,1417,126],{"class":108},[74,1419,113],{"class":112},[74,1421,109],{"class":108},[74,1423,113],{"class":112},[74,1425,471],{"class":108},[74,1427,1428,1430,1432,1434,1436,1439],{"class":76,"line":119},[74,1429,113],{"class":84},[74,1431,109],{"class":108},[74,1433,163],{"class":112},[74,1435,126],{"class":108},[74,1437,1438],{"class":168},"1.2",[74,1440,132],{"class":108},[74,1442,1443,1445,1447,1449,1451,1453,1455,1457],{"class":76,"line":135},[74,1444,177],{"class":84},[74,1446,102],{"class":101},[74,1448,1415],{"class":315},[74,1450,126],{"class":108},[74,1452,113],{"class":112},[74,1454,109],{"class":108},[74,1456,113],{"class":112},[74,1458,471],{"class":108},[74,1460,1461],{"class":76,"line":140},[74,1462,92],{"emptyLinePlaceholder":91},[74,1464,1465,1467,1469,1471,1473,1475],{"class":76,"line":156},[74,1466,123],{"class":122},[74,1468,126],{"class":108},[74,1470,177],{"class":112},[74,1472,199],{"class":101},[74,1474,202],{"class":112},[74,1476,132],{"class":108},[19,1478,1479],{},"More precise:",[65,1481,1483],{"className":67,"code":1482,"language":69,"meta":70,"style":70},"import time\n\nstart = time.time()\ntime.sleep(1.2)\nend = time.time()\n\nprint(end - start)\n",[14,1484,1485,1491,1495,1509,1523,1537,1541],{"__ignoreMap":70},[74,1486,1487,1489],{"class":76,"line":77},[74,1488,81],{"class":80},[74,1490,85],{"class":84},[74,1492,1493],{"class":76,"line":88},[74,1494,92],{"emptyLinePlaceholder":91},[74,1496,1497,1499,1501,1503,1505,1507],{"class":76,"line":95},[74,1498,143],{"class":84},[74,1500,102],{"class":101},[74,1502,105],{"class":84},[74,1504,109],{"class":108},[74,1506,113],{"class":112},[74,1508,116],{"class":108},[74,1510,1511,1513,1515,1517,1519,1521],{"class":76,"line":119},[74,1512,113],{"class":84},[74,1514,109],{"class":108},[74,1516,163],{"class":112},[74,1518,126],{"class":108},[74,1520,1438],{"class":168},[74,1522,132],{"class":108},[74,1524,1525,1527,1529,1531,1533,1535],{"class":76,"line":135},[74,1526,177],{"class":84},[74,1528,102],{"class":101},[74,1530,105],{"class":84},[74,1532,109],{"class":108},[74,1534,113],{"class":112},[74,1536,116],{"class":108},[74,1538,1539],{"class":76,"line":140},[74,1540,92],{"emptyLinePlaceholder":91},[74,1542,1543,1545,1547,1549,1551,1553],{"class":76,"line":156},[74,1544,123],{"class":122},[74,1546,126],{"class":108},[74,1548,177],{"class":112},[74,1550,199],{"class":101},[74,1552,202],{"class":112},[74,1554,132],{"class":108},[60,1556,1558,1559],{"id":1557},"useful-commands-to-test-timetime","Useful commands to test ",[14,1560,16],{},[19,1562,1563,1564,1566],{},"You can test ",[14,1565,16],{}," quickly from your terminal.",[19,1568,1569],{},"Check your Python version:",[65,1571,1575],{"className":1572,"code":1573,"language":1574,"meta":70,"style":70},"language-bash shiki shiki-themes material-theme-lighter github-light github-dark","python --version\n","bash",[14,1576,1577],{"__ignoreMap":70},[74,1578,1579,1582],{"class":76,"line":77},[74,1580,69],{"class":1581},"sbgvK",[74,1583,1585],{"class":1584},"stzsN"," --version\n",[19,1587,1588],{},"Print the current timestamp:",[65,1590,1592],{"className":1572,"code":1591,"language":1574,"meta":70,"style":70},"python -c \"import time; print(time.time())\"\n",[14,1593,1594],{"__ignoreMap":70},[74,1595,1596,1598,1601,1603,1606],{"class":76,"line":77},[74,1597,69],{"class":1581},[74,1599,1600],{"class":1584}," -c",[74,1602,960],{"class":347},[74,1604,1605],{"class":351},"import time; print(time.time())",[74,1607,1608],{"class":347},"\"\n",[19,1610,1611],{},"Measure about 1 second of sleep:",[65,1613,1615],{"className":1572,"code":1614,"language":1574,"meta":70,"style":70},"python -c \"import time; start=time.time(); time.sleep(1); print(time.time()-start)\"\n",[14,1616,1617],{"__ignoreMap":70},[74,1618,1619,1621,1623,1625,1628],{"class":76,"line":77},[74,1620,69],{"class":1581},[74,1622,1600],{"class":1584},[74,1624,960],{"class":347},[74,1626,1627],{"class":351},"import time; start=time.time(); time.sleep(1); print(time.time()-start)",[74,1629,1608],{"class":347},[19,1631,1632],{},"Check the return type:",[65,1634,1636],{"className":1572,"code":1635,"language":1574,"meta":70,"style":70},"python -c \"import time; print(type(time.time()))\"\n",[14,1637,1638],{"__ignoreMap":70},[74,1639,1640,1642,1644,1646,1649],{"class":76,"line":77},[74,1641,69],{"class":1581},[74,1643,1600],{"class":1584},[74,1645,960],{"class":347},[74,1647,1648],{"class":351},"import time; print(type(time.time()))",[74,1650,1608],{"class":347},[19,1652,1653,1654,1658],{},"If you are new to troubleshooting, the ",[54,1655,1657],{"href":1656},"\u002Fhow-to\u002Fhow-to-debug-python-code-beginner-guide","how to debug Python code beginner guide"," is a good next step.",[60,1660,1662],{"id":1661},"faq","FAQ",[1078,1664,1666,1667,1669],{"id":1665},"what-does-timetime-return-in-python","What does ",[14,1668,16],{}," return in Python?",[19,1671,1672],{},"It returns the current time as a float representing seconds since the Unix epoch.",[1078,1674,1676,1677,1679],{"id":1675},"why-does-timetime-return-a-large-number","Why does ",[14,1678,16],{}," return a large number?",[19,1681,1682],{},"Because it counts seconds from January 1, 1970 UTC to the current moment.",[1078,1684,1686,1687,1689],{"id":1685},"is-timetime-good-for-measuring-code-speed","Is ",[14,1688,16],{}," good for measuring code speed?",[19,1691,1692],{},"It is fine for simple timing, but more precise tools are better for benchmarking.",[1078,1694,1696,1697,1699],{"id":1695},"how-do-i-get-whole-seconds-from-timetime","How do I get whole seconds from ",[14,1698,16],{},"?",[19,1701,1702,1703,1705],{},"Wrap the result with ",[14,1704,519],{}," if you only want the integer part.",[65,1707,1709],{"className":67,"code":1708,"language":69,"meta":70,"style":70},"import time\n\nprint(int(time.time()))\n",[14,1710,1711,1717,1721],{"__ignoreMap":70},[74,1712,1713,1715],{"class":76,"line":77},[74,1714,81],{"class":80},[74,1716,85],{"class":84},[74,1718,1719],{"class":76,"line":88},[74,1720,92],{"emptyLinePlaceholder":91},[74,1722,1723,1725,1727,1729,1731,1733,1735,1737],{"class":76,"line":95},[74,1724,123],{"class":122},[74,1726,126],{"class":108},[74,1728,567],{"class":315},[74,1730,126],{"class":108},[74,1732,113],{"class":112},[74,1734,109],{"class":108},[74,1736,113],{"class":112},[74,1738,1739],{"class":108},"()))\n",[60,1741,1743],{"id":1742},"see-also","See also",[37,1745,1746,1750,1755,1759,1764,1769],{},[40,1747,1748],{},[54,1749,1018],{"href":1017},[40,1751,1752],{},[54,1753,1754],{"href":772},"time.sleep() function explained",[40,1756,1757],{},[54,1758,57],{"href":56},[40,1760,1761],{},[54,1762,1763],{"href":579},"datetime.now() explained",[40,1765,1766],{},[54,1767,1768],{"href":1656},"How to debug Python code: beginner guide",[40,1770,1771],{},[54,1772,1773,1774,1776],{"href":516},"Python ",[14,1775,519],{}," function explained",[19,1778,1779,1780,1782,1783,1785,1786,109],{},"Use ",[14,1781,16],{}," when you need a timestamp or a simple elapsed-time check. If you need readable dates, move to ",[14,1784,1061],{},". If you need to pause your program, use ",[14,1787,775],{},[1789,1790,1791],"style",{},"html pre.shiki code .sVHd0, html code.shiki .sVHd0{--shiki-light:#39ADB5;--shiki-light-font-style:italic;--shiki-default:#D73A49;--shiki-default-font-style:inherit;--shiki-dark:#F97583;--shiki-dark-font-style:inherit}html pre.shiki code .su5hD, html code.shiki .su5hD{--shiki-light:#90A4AE;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .smGrS, html code.shiki .smGrS{--shiki-light:#39ADB5;--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .sP7_E, html code.shiki .sP7_E{--shiki-light:#39ADB5;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .slqww, html code.shiki .slqww{--shiki-light:#6182B8;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sptTA, html code.shiki .sptTA{--shiki-light:#6182B8;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .srdBf, html code.shiki .srdBf{--shiki-light:#F76D47;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html .light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html.light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html pre.shiki code .sZMiF, html code.shiki .sZMiF{--shiki-light:#E2931D;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sbsja, html code.shiki .sbsja{--shiki-light:#9C3EDA;--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .sjJ54, html code.shiki .sjJ54{--shiki-light:#39ADB5;--shiki-default:#032F62;--shiki-dark:#9ECBFF}html pre.shiki code .s_sjI, html code.shiki .s_sjI{--shiki-light:#91B859;--shiki-default:#032F62;--shiki-dark:#9ECBFF}html pre.shiki code .s_hVV, html code.shiki .s_hVV{--shiki-light:#90A4AE;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sbgvK, html code.shiki .sbgvK{--shiki-light:#E2931D;--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .stzsN, html code.shiki .stzsN{--shiki-light:#91B859;--shiki-default:#005CC5;--shiki-dark:#79B8FF}",{"title":70,"searchDepth":88,"depth":88,"links":1793},[1794,1795,1797,1798,1799,1800,1802,1803,1813,1815,1825],{"id":62,"depth":88,"text":63},{"id":230,"depth":88,"text":1796},"What time.time() does",{"id":363,"depth":88,"text":364},{"id":480,"depth":88,"text":481},{"id":589,"depth":88,"text":590},{"id":782,"depth":88,"text":1801},"Using time.time() to measure elapsed time",{"id":1021,"depth":88,"text":1022},{"id":1070,"depth":88,"text":1071,"children":1804},[1805,1807,1808,1810,1812],{"id":1080,"depth":95,"text":1806},"Forgetting to import the time module",{"id":1166,"depth":95,"text":1167},{"id":1212,"depth":95,"text":1809},"Confusing time.time() with time.sleep()",{"id":1313,"depth":95,"text":1811},"Calling time() directly after import time",{"id":1381,"depth":95,"text":1382},{"id":1557,"depth":88,"text":1814},"Useful commands to test time.time()",{"id":1661,"depth":88,"text":1662,"children":1816},[1817,1819,1821,1823],{"id":1665,"depth":95,"text":1818},"What does time.time() return in Python?",{"id":1675,"depth":95,"text":1820},"Why does time.time() return a large number?",{"id":1685,"depth":95,"text":1822},"Is time.time() good for measuring code speed?",{"id":1695,"depth":95,"text":1824},"How do I get whole seconds from time.time()?",{"id":1742,"depth":88,"text":1743},"Master time.time function explained in our comprehensive Python beginner guide.","md",{},"\u002Fstandard-library\u002Ftime.time-function-explained",{"title":5,"description":1826},"standard-library\u002Ftime.time-function-explained","tBvmudHhfLdisypsNQJljwOze5fjFNRKRBJoEqauyUI",1777585471226]