C語言時間處理執行個體分享_C 語言

來源:互聯網
上載者:User

一、簡介

時間處理在編程中經常遇到,包括程式的已耗用時間和顯示時間等。在標準C中, 日期和時間的處理包含在 time.h 的標頭檔中,需要使用日期和時間相關的類型的函數的話, 需要匯入time.h.

二、執行個體

1、計算時差

#include <stdio.h>                                         #include <sys/time.h>   #include <unistd.h>    int main(){  struct timeval start, end;  unsigned long spend_time;  gettimeofday( &start, NULL );  printf("start : %d.%d\n", start.tv_sec, start.tv_usec);  sleep(1);  gettimeofday( &end, NULL );  printf("end  : %d.%d\n", end.tv_sec, end.tv_usec);  //微秒時差  spend_time=1000000*(end.tv_sec-start.tv_sec)+(end.tv_usec-start.tv_usec);  printf("%ld\n",spend_time);  return 0;}

編譯

gcc  -g -o time_diff time_diff.c
運行

以上所述就是本文的全部內容了,希望大家能夠喜歡。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.