Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C++

ฟังก์ชัน difftime() ของไลบรารี C

นี่คือฟังก์ชัน difftime() ใน C ใช้หาความต่างระหว่างสองค่าเวลา

difftime() รับอาร์กิวเมนต์เวลาสองค่า ค่าแรกคือ lower bound ค่าที่สองคือ upper bound และคืนค่าความต่างระหว่างสองอาร์กิวเมนต์นี้

ตัวอย่าง

#include <time.h>
#include <stdio.h>
#include <unistd.h>

main() {
  int sec;
  time_t time1, time2;
  time(&time1);
  printf("Current Time: %ld\n", time1);
  for (sec = 1; sec <= 5; sec++) {
    sleep(1);
    printf("Count: %d\n", sec);
  }
  time(&time2);
  printf("Ending Time: %ld\n", time2);
  printf("Difference is %.2f seconds", difftime(time2, time1));
}

ผลลัพธ์

Current Time: 1554918635
Count: 1
Count: 2
Count: 3
Count: 4
Count: 5
Ending Time: 1554918640
Difference is 5.00 seconds