??xml version="1.0" encoding="utf-8" standalone="yes"?>
参? http://otl.sourceforge.net/otl3_stream_class.htm
50 2.06
<1:536704> 18:31:58.068 [00001] The load SQL is [select USER_NUMBER from dr_gsm_931_20050831 where 1=1 ]
<1:536704> 18:34:04.713 [00001] There are 3612066 records is loaded!
100 1.38
<1:2085112> 18:36:23.727 [00001] The load SQL is [select USER_NUMBER from dr_gsm_931_20050831 where 1=1 ]
<1:2085112> 18:38:01.923 [00001] There are 3612066 records is loaded!
1.23
<1:2085112> 18:38:06.941 [00001] The load SQL is [select USER_NUMBER from dr_gsm_931_20050831 where 1=1 ]
<1:2085112> 18:39:29.849 [00001] There are 3612066 records is loaded!
200 1.02
<1:1499250> 18:41:47.711 [00001] The load SQL is [select USER_NUMBER from dr_gsm_931_20050831 where 1=1 ]
<1:1499250> 18:42:49.300 [00001] There are 3612066 records is loaded!
<1:1499250> 18:42:54.312 [00001] The load SQL is [select USER_NUMBER from dr_gsm_931_20050831 where 1=1 ]
<1:1499250> 18:43:53.717 [00001] There are 3612066 records is loaded!
500 0.43
<1:1978536> 18:45:57.790 [00001] The load SQL is [select USER_NUMBER from dr_gsm_931_20050831 where 1=1 ]
<1:1978536> 18:46:40.685 [00001] There are 3612066 records is loaded!
<1:1978536> 18:45:09.982 [00001] The load SQL is [select USER_NUMBER from dr_gsm_931_20050831 where 1=1 ]
<1:1978536> 18:45:52.787 [00001] There are 3612066 records is loaded!
1000 0.35
<1:1597632> 18:47:42.973 [00001] The load SQL is [select USER_NUMBER from dr_gsm_931_20050831 where 1=1 ]
<1:1597632> 18:48:17.715 [00001] There are 3612066 records is loaded!
<1:1597632> 18:48:22.718 [00001] The load SQL is [select USER_NUMBER from dr_gsm_931_20050831 where 1=1 ]
<1:1597632> 18:49:01.119 [00001] There are 3612066 records is loaded!
<1:1597632> 18:51:17.837 [00001] The load SQL is [select USER_NUMBER from dr_gsm_931_20050831 where 1=1 ]
<1:1597632> 18:51:24.416 [10001] Signal 31 recieved.
<1:1597632> 18:49:49.976 [00001] The load SQL is [select USER_NUMBER from dr_gsm_931_20050831 where 1=1 ]
<1:1597632> 18:50:28.192 [00001] There are 3612066 records is loaded!
2000 0.34
<1:2883830> 18:51:47.365 [00001] The load SQL is [select USER_NUMBER from dr_gsm_931_20050831 where 1=1 ]
<1:2883830> 18:52:21.163 [00001] There are 3612066 records is loaded!
<1:2883830> 18:52:26.166 [00001] The load SQL is [select USER_NUMBER from dr_gsm_931_20050831 where 1=1 ]
<1:2883830> 18:53:01.576 [00001] There are 3612066 records is loaded!
Chuck Allison是盐湖城?/span>Latter Day教堂总部下耶稣教堂家族历史研究处的软g体系设计?/span>。他拥有数学学士和数学硕士学位。他?/span>1975qv开始编E,?/span>1984qv他开始从?/span>c语言的教学和开发。他目前的兴是面向对象的技术及其教肌Ӏ他?/span>X3J16Q?/span>ANSI C ++标准化委员会的一员?/span>发?/span>e-mail?/span>allison@decus.orgQ或者拨打电话到(801)240-4510均可以与他取得联pR?/span>
大部分的操作pȝ有办法得到当前的日期和时间。通过定义?em>time.h的库函数Q?span>ANSI C能以许多不同的Ş式得到这个信息。函?em>timeq回一个类型ؓtime_t的|通常为longQ,该函数在q行期间对当前的日期和时间进行编码。然后你可以这个返回g递给其他能对该D行解码和格式化的函数?/span>
Listing 1中的E序使用函数timeQ?em>localtime?em>strftime以不同的形式输出当前的日期和旉。函?em>localtime把已l编码的旉解码成如下的structQ?/font>
struct tm
{
int tm_sec; /* (0 - 61) */
int tm_min; /* (0 - 59) */
int tm_hour; /* (0 - 23) */
int tm_mday; /* (1 - 31) */
int tm_mon; /* (0 - 11) */
int tm_year; /* past 1900 */
int tm_wday; /* (0 - 6) */
int tm_yday; /* (0 - 365) */
int tm_isdst; /* daylight savings flag */
};
通过改变tml构里的|可对旉/日期q行q算?a >Listing 2中的E序展示了如何计将来某天的日期和以Uؓ单位所计算出的E序执行旉。注意函?em>time的语法(参数time_t由地址传入Qƈ非作为函数的q回|。函?em>mktime改变tml构的|以便日期和时间在一个合适的范围内,之后day-of-week (tm_wday)和day-of-year (tm_yday)域进行相应的更新?em>mktime?em>tml构中日期和旉的值置于合适的范围之内Q相应的更新day of week (tm-wday)和day of year (tm-yday)的倹{这U情况发生在当一个日期超Z你的实现能够支持的范围的时候。例如,我的MS-DOS的编译器不能~码1970q?月䆾之前的日期。函?em>asctimeq回tm参数所描述旉的标准字W串Q因?em>ctime (&tval)与asctime (localtime(&tval)是相{的)。函?em>difftimeq回用秒做单位的两个time_t的差?/span>
如果需要处理超出系l范围的日期Q或者需要计两个日期的间隔又不是用U来做单位,那你需要设计自qdate~码?a >Listing 3 ?a >Listing 5中的应用E序通过使用一个简单的month-day-yearl构Q展CZ定两个日期间隔的年数、月份数和天数的技术。日期的相减像你在学里做的减法那P例如Q首先进行天数的相减Q如果需要就向月份数借位Q以此类推)。注意蟩q的q䆾都被计算q去了。ؓ了简略v见,date_interval函数假设日期都是有效的,q且W一个日期在W二个日期之前。函数返回一个指向静?em>Datel构的指针,该结构包含了我们惌的答案?/span>
大多数操作系lؓ文gl护旉/日期戟뀂至你能得知一个文件最后被修改的时间。(常用?em>make工具使用q一信息来决定一个文件是否需要被重新~译Q或者一个应用程序是否需要被重新q接Q。由于文件系l在不同q_上有所不同Q没有什么通用的函数得C个文件的旉/日期戻I因此ANSI 标准没有定义q样的函数。然而,大多数流行的操作pȝQ包括MS-DOS和VAX/VMSQ提供了UNIX函数statQ该函数q回相关的文件信息,包括?em>time_t表示的最后修Ҏ间?/span>
Listing 6中的E序使用stat?em>difftime来确定是?em>time1.c?em>time2.c更新Q例如,是否最q被修改q)?/font>
如果你需要更C个文件的旉/日期戛_当前旉Q可单的重写文g的第一个字节。虽然实际内容ƈ未改变,但你的文件系l会认ؓ文g已经被改变了Qƈ且会相应的更新时?日期戟뀂(知道你的文gpȝQ在VAX/VMS下,当你得到一个文件的新版本的时候,旧的版本仍会被保留)。这U技术叫?#8220;‘touching’一个文?#8221;?a >Listing 7?em>touch的实现在指定文g不存在的时候会创徏一个新文g。注意文件以“binary”模式打开Q在打开模式字符串中由字W?em>b军_—在来的专栏中我会详细讨论文g处理的问题)?/span>
Code Sample Output
---------------------------------------------
%a Wed
%A Wednesday
%b Oct
%B October
%c Wed Oct 07 13:24:27 1992
%d 07 (day of month [01-31])
%H 13 (hour in [00-23])
%I 01 (hour in [01-12])
%j 281 (day of year [001-366])
%m 10 (month [01-12])
%M 24 (minute [00-59])
%p PM
%S 27 (second [00-59] )
%U 40 (Sunday week of year [00-52])
%w 3 (day of week [0-6])
%W 40 (Monday week of year [00-52])
%x Wed Oct 7, 1992
%X 13:24:27
%y 92
%Y 1992
%Z EDT (daylight savings indicator)
#include <stdio.h>
#include <time.h>
#define BUFSIZE 128
main()
{
time_t tval;
struct tm *now;
char buf[BUFSIZE];
char *fancy_format =
"Or getting really fancy:\n"
"%A, %B %d, day %j of %Y.\n"
"The time is %I:%M %p.";
/* Get current date and time */
tval = time(NULL);
now = localtime(&tval);
printf("The current date and time:\n"
"%d/%02d/%02d %d:%02d:%02d\n\n",
now->tm_mon+1, now->tm_mday, now->tm_year,
now->tm_hour, now->tm_min, now->tm_sec);
printf("Or in default system format:\n%s\n",
ctime(&tval));
strftime(buf,sizeof buf,fancy_format,now);
puts(buf);
return 0;
}
/* Output
The current date and time:
10/06/92 12:58:00
Or in default system format:
Tue Oct 06 12:58:00 1992
Or getting really fancy:
Tuesday, October 06, day 280 of 1992.
The time is 12:58 PM.
*/
/* End of File */
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
main()
{
time_t start, stop;
struct tm *now;
int ndays;
/* Get current date and time */
time(&start);
now = localtime(&start);
/* Enter an interval in days */
fputs("How many days from now? ",stderr);
if (scanf("%d",&ndays) !=1)
return EXIT_FAILURE;
now->tm_mday += ndays;
if (mktime(now) != -1)
printf("New date: %s",asctime(now));
else
puts("Sorry. Can't encode your date.");
/* Calculate elapsed time */
time(&stop);
printf("Elapsed program time in seconds: %f\n",
difftime(stop,start));
return EXIT_SUCCESS;
}
/* Output
How many days from now? 45
New date: Fri Nov 20 12:40:32 1992
Elapsed program time in seconds: 1.000000
*/
/* End of File */
struct Date
{
int day;
int month;
int year;
};
typedef struct Date Date;
Date* date_interval(const Date *, const Date *);
/* End of File */
/* date_int.c: Compute duration between two dates */
#include "date.h"
#define isleap(y) \
((y)%4 == 0 && (y)%100 != 0 || (y)%400 == 0)
static int Dtab [2][13] =
{
{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
{0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
};
Date *date_interval(const Date *d1, const Date *d2)
{
static Date result;
int months, days, years, prev_month;
/* Compute the interval - assume d1 precedes d2 */
years = d2->year - d1->year;
months = d2->month - d1->month;
days = d2->day - d1->day;
/* Do obvious corrections (days before months!)
*
* This is a loop in case the previous month is
* February, and days < -28.
*/
prev_month = d2->month - 1;
while (days < 0)
{
/* Borrow from the previous month */
if (prev_month == 0)
prev_month = 12;
--months;
days += Dtab[isleap(d2->year)][prev_month--];
}
if (months < 0)
{
/* Borrow from the previous year */
--years;
months += 12;
}
/* Prepare output */
result.month = months;
result.day = days;
result.year = years;
return &result;
}
/* End of File */
/* tdate.c: Test date_interval() */
#include <stdio.h>
#include <stdlib.h>
#include "date.h"
main()
{
Date d1, d2, *result;
int nargs;
/* Read in two dates - assume 1st precedes 2nd */
fputs("Enter a date, MM/DD/YY> ",stderr);
nargs = scanf("%d/%d/%d%*c", &d1.month,
&d1.day, &d1.year);
if (nargs != 3)
return EXIT_FAILURE;
fputs("Enter a later date, MM/DD/YY> ",stderr);
nargs = scanf("%d/%d/%d%*c", &d2.month,
&d2.day, &d2.year);
if (nargs != 3)
return EXIT_FAILURE;
/* Compute interval in years, months, and days */
result = date_interval(&d1, &d2);
printf("years: %d, months: %d, days: %d\n",
result->year, result->month, result->day);
return EXIT_SUCCESS;
}
/* Sample Execution:
Enter a date, MM/DD/YY> 10/1/51
Enter a later date, MM/DD/YY> 10/6/92
years: 41, months: 0, days: 5 */
/* End of File */
/* ftime.c: Compare file time stamps */
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <time.h>
main()
{
struct stat fs1, fs2;
if (stat("time1.c",&fs1) == 0 &&
stat("time2.c",&fs2) == 0)
{
double interval =
difftime(fs2.st_mtime,fs1.st_mtime);
printf("time1.c %s newer than time2.c\n",
(interval < 0.0) ? "is" : "is not");
return EXIT_SUCCESS;
}
else
return EXIT_FAILURE;
}
/* Output
time1.c is not newer than time2.c */
/* End of File */
/* touch.c: Update a file's time stamp */
#include <stdio.h>
void touch(char *fname)
{
FILE *f = fopen(fname,"r+b");
if (f != NULL)
{
char c = getc(f);
rewind(f);
putc(c,f);
}
else
fopen(fname,"wb");
fclose(f);
}
/* End of File */
![]() |
|
|
q段E序的功能很单,是定义了三个结构X1,X2,X3,q三个结构的主要区别是内存数据摆放的顺序,其他都是一LQ另外程序输入了几种基本cd所占用的字节数Q以及我们这里的三个l构所占用的字节数?br />
q段E序的运行结果ؓQ?br />
l果的前面四行没有什么问题,但是我们在最后三行就可以看到三个l构占用的空间大不一P造成q个原因是内部数据的摆N序,怎么会这样呢Q?br />
下面是我们需要讲的内存对齐了?br />
内存是一个连l的块,我们可以用下面的图来表示, 它是?个字节对一个对齐单位的Q?br />
图一
让我们看看三个结构在内存中的布局Q?br />
首先?X1Q如下图所C?br />
X1 中第一个是 IntcdQ它占有4字节Q所以前?格就是满了,然后W二个是charcdQ这中类型只占一个字节,所以它占有了第二个4字节l块中的W一|W三个也是charcdQ所以它也占用一个字节,它就排在了第二个l块的第二格Q因为它们加在一起大也不超q一个块Q所以他们三个变量在内存中的l构是q样的,因ؓ有内存分块对齐,所以最后出来的l果?Q而不?Q因为后面两个格子其实也是被用了?br />
再次看看X2Q如图所C?br />
X2中第一个类型是CharcdQ它占用一个字节,所以它首先排在W一l块的第一个格子里面,W二个是IntcdQ它占用4个字节,W一l块已经用掉一|q剩3|肯定是无法放下第二Intcd的,因ؓ要考虑到对齐,所以不得不把它攑ֈW二个组块,W三个类型是CharcdQ跟W一个类伹{所因ؓ有内存分块对齐,我们的内存就不是8个格子了Q而是12个了?br />
再看看X3Q如下图所C:
关于X3的说明其实跟X1是类似的Q只不过它把两个1个字节的攑ֈ了前面,怿看了前面两种情况的说明这里也是很Ҏ理解的?br />