Computer >> कंप्यूटर ट्यूटोरियल >  >> प्रोग्रामिंग >> C++

C++ में difftime() फ़ंक्शन


इस लेख में हम C++ में difftime() फ़ंक्शन, इसके सिंटैक्स, कार्य और इसके वापसी मानों पर चर्चा करेंगे।

difftime() फ़ंक्शन C++ में एक अंतर्निहित फ़ंक्शन है जो हेडर फ़ाइल में परिभाषित है। फ़ंक्शन time_t प्रकार के दो पैरामीटर स्वीकार करता है, फ़ंक्शन दो समयों के बीच का अंतर 계산 करता है।

सिंटैक्स

double difftime(time_t end, time_t beginning);

वापसी मान

सेकंड में समय का अंतर लौटाता है, जिसे double डेटा प्रकार के रूप में संग्रहीत किया जाता है।

उदाहरण

#include <stdio.h> #include <time.h> int main () {    time_t now;    struct tm newyear;    double seconds;    time(&now); /* get current time; */    newyear = *localtime(&now);    newyear.tm_hour = 0; newyear.tm_min = 0; newyear.tm_sec = 0;    newyear.tm_mon = 0; newyear.tm_mday = 1;    seconds = difftime(now,mktime(&newyear));    printf ("%.f seconds since new year in the current timezone.\n", seconds);    return 0; }

आउटपुट

यदि हम उपरोक्त कोड चलाते हैं तो यह निम्नलिखित आउटपुट उत्पन्न करेगा −

3351041 seconds since new year in the current timezone.

उदाहरण

#include <iostream> #include <ctime> using namespace std; int main() {    time_t start, ending;    long addition;    time(&start);    for (int i = 0;    i < 50000; i++) {       for (int j = 0; j < 50000; j++);    }    for (int i = 0; i < 50000; i++) {       for (int j = 0; j < 50000; j++);    } for (int i = 0; i < 50000; i++) {       for (int j = 0; j < 50000; j++);    } time(&ending);    cout << "Total time required = " << difftime(ending, start) << " seconds " << endl;    return 0; }

आउटपुट

यदि हम उपरोक्त कोड चलाते हैं तो यह निम्नलिखित आउटपुट उत्पन्न करेगा −

Total time required = 37 seconds

  1. - C++ में transform() फ़ंक्शन

    परिचय transform फ़ंक्शन C++ STL (स्टैंडर्ड टेम्पलेट लाइब्रेरी) में मौजूद है। इसका उपयोग करने के लिए हमें <algorithm> हेडर फाइल शामिल करनी होती है। यह फ़ंक्शन सभी एलिमेंट्स पर एक ऑपरेशन करने के लिए उपयोग किया जाता है। उदाहरण के लिए, यदि हम एक ऐरे के प्रत्येक एलिमेंट का वर्ग निकालकर दूसरे ऐरे मे

  1. - C++ में log() फ़ंक्शन

    C/C++ लाइब्रेरी फ़ंक्शन double log(double x) x का नेचुरल लॉगरिदम (बेस e लॉगरिदम) लौटाता है। log() फ़ंक्शन के लिए घोषणा निम्नलिखित है। double log(double x) पैरामीटर एक फ्लोटिंग पॉइंट मान है। और यह फ़ंक्शन x का नेचुरल लॉगरिदम लौटाता है। उदाहरण #include <iostream> #include <cmath> using

  1. - C++ में swap() फ़ंक्शन

    swap() फ़ंक्शन दो संख्याओं को स्वैप (अदला-बदली) करने के लिए उपयोग किया जाता है। तीसरे वेरिएबल की आवश्यकता नहीं होती। <algorithm> या <utility> हेडर में परिभाषित (C++11 से)। सिंटैक्स void swap(Type& a, Type& b); उदाहरण 1: मान द्वारा पास (मान बदलते नहीं) #include <bits/stdc++.