Vous êtes sur la page 1sur 4

CL103-Computer Programming Lab

Lab # ##: Timing & Delay Functions


Objectives:

Windows Sleep Function


Measuring Computational Cost
Time & Date Functions

Instructions:

You are allowed to use your own laptops.


You can consult the books, manuals and class lectures.
You should have stationary like register and ballpoint to analyze the tasks first.
Ensure that you working environment is working properly.
Only in practice session, consultation is allowed.
Attempt all questions yourself.
No discussion is allowed during tasks solution.
Submission of all tasks solutions is necessary in any way.
Tasks completed in specified time will be graded.
Cheating is strictly prohibited otherwise rules as per university will be applied as a result of F-
Grade in lab.
Lab # ##: Timing & Delay Functions Page 2 of 4
Windows Sleep Function

Getting Started

Windows Sleep Function


//------------------------Mr. Najeeb-Ur-Rehman-------------------------------
//------------------------CREATION DATE: 12/04/2012--------------------------
//------------------------Title: Windows Sleep Function ---------------------
// This example prompts for
//to practice the Sleep() function
// to put a delay between two statements...
#include <iostream>
#include <conio.h>
#include <windows.h>

using namespace std;


void main()
{
cout<<"\n\n\t\tWelcome to Time Wasting Programming ... !!!"<<endl;
cout<<"\n\tYou have to wait for 3 seconds...";
Sleep(3000); //1000=1-Second and realativly you can shange thes
//keep in mind that it is case senstive as 'S' is capital in sleep
function...
cout<<"\n\tYour wating time has been ended now."<<endl;
cout<<"\n\n\n\tTrying to build a delay with progress bar ";
int oneSecond=1000;
for(int i=0; i<10;i++)
{
Sleep(oneSecond);
cout<<". ";
}
cout<<"\n\n\t\t\tEnd of wait \n\n";
system("PAUSE");
}

/**
Sample Output-1
Welcome to Time Wasting Programming ... !!!

You have to wait for 3 seconds...


Your wating time has been ended now.

Trying to build a delay with progress bar . . . . . . . . . .

End of wait

Press any key to continue . . .


*/

Measuring Computational Cost


//------------------------Mr. Najeeb-Ur-Rehman-------------------------------
//------------------------CREATION DATE: 12/04/2012--------------------------
//------------------------Title: Measuring Computational Cost----------------
// This example prompts for how long
// the program is to run and then continuously

CL103-Computer Programming Lab By: Mr. Najeeb -Ur-Rehman


Lab # ##: Timing & Delay Functions Page 3 of 4
Measuring Computational Cost

// displays the elapsed time for that period.


//
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

void sleep( clock_t wait );

int main( void )


{
long i = 30000000L;
clock_t start, finish;
double duration;

// Delay for a specified time.


printf( "Delay for three seconds\n" );
sleep( (clock_t)3 * CLOCKS_PER_SEC );
printf( "Done!\n" );

// Measure the duration of an event.


printf( "Time to do %ld empty loops is ", i );
start = clock();
while( i-- )
;
sleep(3000);
finish = clock();
duration = (double)(finish - start) / CLOCKS_PER_SEC;
printf( "%2.1f seconds\n", duration );
}
// Pauses for a specified number of milliseconds.
void sleep( clock_t wait )
{
clock_t goal;
goal = wait + clock();
while( goal > clock() )
;
}

/**
Sample Output-1
Delay for three seconds
Done!
Time to do 60000000 empty loops is 0.1 seconds
Press any key to continue . . .
Sample Output-2
Delay for three seconds
Done!
Time to do 30000000 empty loops is 0.1 seconds
Press any key to continue . . .
*/

CL103-Computer Programming Lab By: Mr. Najeeb -Ur-Rehman


Lab # ##: Timing & Delay Functions Page 4 of 4
Time & Date Functions

Time & Date Functions


// This program demonstrates these time and date functions:

#include <time.h>
#include <iostream>
using namespace std;
int main()
{
// Display operating system-style date and time.
// help resource ...
//ms-help://MS.VSCC.v90/MS.msdnexpress.v90.en/dv_vccrt/html/280e00f2-
2b93-4ece-94cd-e048484c6cc7.htm
char tmpbuf[128];;
_strtime_s( tmpbuf, 128 );
cout<<"OS Time\t:\t"<<tmpbuf;
_strdate_s( tmpbuf, 128 );
cout<<"\nOS Date\t:\t"<<tmpbuf<<endl<<endl;
return (0);
}
/*
1. Sample Output...
OS Time : 08:55:57
OS Date : 04/12/12
2. Sample Output...
OS Time : 08:56:34
OS Date : 04/12/12
*/

CL103-Computer Programming Lab By: Mr. Najeeb -Ur-Rehman

Vous aimerez peut-être aussi