为什么difftime()返回一个double?

时间:2015-12-24 21:10:51

标签: c posix

来自here

  

testing: warning: no tests to run PASS BenchmarkGoLogging-4 1000000 2068 ns/op BenchmarkGoLoggingNullBackend-4 5000000 308 ns/op BenchmarkGoLoggingNullBackendWithFancyFormatter-4 3000000 435 ns/op BenchmarkGoLoggingOffLevel-4 20000000 109 ns/op BenchmarkGoLoggingNullBackendAndOffLevel-4 20000000 108 ns/op BenchmarkGoLoggingNullBackendWithFancyFormatterAndOffLevel-4 20000000 109 ns/op BenchmarkLog15-4 200000 7359 ns/op BenchmarkLog15WithDiscardHandler-4 2000000 922 ns/op BenchmarkLog15WithDiscardHandlerAndOffLevel-4 2000000 926 ns/op BenchmarkLog15WithNopLogger-4 20000000 108 ns/op BenchmarkLog15WithNopLoggerDiscardHandlerA-4 20000000 112 ns/op BenchmarkLog15WithNopLoggerAndDiscardHandlerAndOffLevel-4 20000000 112 ns/op BenchmarkLog-4 1000000 1217 ns/op BenchmarkLogIoDiscardWriter-4 2000000 724 ns/op BenchmarkLogIoDiscardWriterWithoutFlags-4 3000000 543 ns/op BenchmarkLogCustomNullWriter-4 2000000 731 ns/op BenchmarkLogCustomNullWriterWithoutFlags-4 3000000 549 ns/op BenchmarkNopLogger-4 20000000 113 ns/op BenchmarkNopLoggerWithoutFlags-4 20000000 112 ns/op BenchmarkLogrus-4 300000 3832 ns/op BenchmarkLogrusWithDiscardWriter-4 500000 3032 ns/op BenchmarkLogrusWithNullFormatter-4 500000 3814 ns/op BenchmarkLogrusWithPanicLevel-4 500000 3872 ns/op BenchmarkLogrusWithDiscardWriterAndPanicLevel-4 500000 3085 ns/op BenchmarkLogrusWithDiscardWriterAndNullFormatterAndPanicLevel-4 500000 3064 ns/op ok log-benchmarks 51.378s go test -bench . 62.17s user 3.90s system 126% cpu 52.065 total

     

double difftime(time_t time1, time_t time0);函数返回之间经过的秒数   时间difftime()和时间time1,表示为time0

由于'秒数'不需要浮点数,为什么这个函数会返回double

2 个答案:

答案 0 :(得分:4)

这个documentation更清楚:

  

在POSIX系统上,time_t以秒为单位测量,difftime相当于算术减法,但C和C ++允许使用time_t的小数单位。

虽然POSIX requires time_t to be an integer type,但对于非POSIX系统,它可能会返回小数秒。

答案 1 :(得分:4)

C允许各种标量数(整数,浮点数)表示时间。它需要是“......能够代表时间的真实类型”C11§7.27.13,

  

clock_t and time_t中可表示的时间范围和精度   实现定义。 C11dr§7.27.14

2 time_t值之间的差异,double提供了不同的宽范围和精度。

  

OP,“由于'秒数'不需要浮点数,为什么这个函数会返回一个双精度数?

[编辑] Linux / posix可能不会使用几秒钟,但其他系统已经这样做了。定义difftime()的C标准选择double并且可以容纳整数秒的累积以及其他OS实现。