কম্পিউটার টিউটোরিয়াল

সি++-এ clock() ফাংশন ব্যবহার


এখানে আমরা দেখব সি++-এ clock() ফাংশন কীভাবে ব্যবহার করা যায়। এটি time.h বা ctime হেডার ফাইলে থাকে। এখানে আমরা একটি প্রসেসের এলাপসড টাইম (elapsed time) বের করতে clock() ফাংশন ব্যবহার করব।

এলাপসড টাইম পেতে, আমরা কাজের শুরুতে clock() ব্যবহার করে টাইম নিব, এবং কাজের শেষে আবার নিব, তারপর পার্থক্য বের করব। এরপর পার্থক্যকে CLOCKS_PER_SEC (প্রতি সেকেন্ডে ক্লক টিকের সংখ্যা) দিয়ে ভাগ করে প্রসেসর টাইম পাব।

উদাহরণ

#include <iostream>
#include <ctime>
using namespace std;

void take_enter() {
    cout << "Press enter to stop the counter" << endl;
    while (1) {
        if (getchar())
            break;
    }
}

int main() {
    // take_enter() ফাংশন কতক্ষণ নিচ্ছে তা হিসাব
    clock_t t;
    t = clock();
    cout << "Timer starts\n";
    take_enter();
    cout << "Timer ends \n";
    t = clock() - t;
    double time_taken = ((double)t) / CLOCKS_PER_SEC; // এলাপসড টাইম হিসাব
    cout << "The program took " << time_taken << " seconds to execute";
}

আউটপুট

Timer starts
Press enter to stop the counter
Timer ends
The program took 3.546 seconds to execute
  1. এক্সেলে CHOOSE ফাংশন ব্যবহার করার পদ্ধতি

  2. এক্সেলে IF ফাংশন ব্যবহার করার পদ্ধতি

  3. এক্সেলে PI ফাংশন ব্যবহারের পদ্ধতি

  4. Excel-এ VLOOKUP মাস্টার করুন: দক্ষ ডেটা রিট্রিভালের সম্পূর্ণ গাইড