我有一个程序,按字典顺序对输入行进行排序,我在K& R中参加了这个练习:
重写read行以在main提供的数组中存储行,而不是调用alloc来维护存储。程序要快多少?
以下是原始程序,使用malloc
维护行的存储。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXLINES 5000
char *lineptr[MAXLINES];
int readlines(char *lineptr[], int nlines);
void writelines(char *lineptr[], int nlines);
void qsort(char *lineptr[], int left, int right);
char *alloc(int n);
#define MAXSIZE 10000
int main(int argc, char *argv[])
{
int nlines;
if((nlines = readlines(lineptr, MAXLINES)) >= 0) {
qsort(lineptr, 0, nlines-1);
writelines(lineptr, nlines);
getchar();
return 0;
}
else {
printf("error: input too big to sort\n");
getchar();
return 1;
}
}
#define MAXLEN 1000
int getline(char *, int);
int readlines(char *lineptr[], int maxlines)
{
int len, nlines;
char *p;
char line[MAXLEN];
nlines = 0;
while((len = getline(line, MAXLEN)) > 0)
if(nlines >= maxlines || (p = alloc(len)) == NULL)
return -1;
else {
line[len-1] = '\0';
strcpy(p, line);
lineptr[nlines++] = p;
}
return nlines;
}
void writelines(char *lineptr[], int nlines)
{
while(nlines-- > 0)
printf("%s\n", *lineptr++);
}
#define MAXALLOC 5000
char allocbuf[MAXALLOC];
char *allocp = allocbuf;
char *alloc(int n)
{
if(allocbuf + MAXALLOC - allocp >= n) {
allocp += n;
return allocp - n;
}
else
return 0;
}
int getline(char s[], int lim)
{
int c, i;
for (i = 0; i < lim - 1 && (c = getchar()) != EOF && c != '\n'; i++)
s[i] = c;
if (c == '\n') {
s[i++] = c;
}
s[i] = '\0';
return i;
}
void swap(char *v[], int i, int j)
{
char *temp;
temp = v[i];
v[i] = v[j];
v[j] = temp;
}
void qsort(char *v[], int left, int right) {
int i, last;
if(left >= right)
return;
swap(v, left, (left+right)/2);
last = left;
for(i = left + 1; i <= right; i++)
if(strcmp(v[i], v[left]) < 0)
swap(v, ++last, i);
swap(v, left, last);
qsort(v, left, last-1);
qsort(v, last+1, right);
}
我这样编辑:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXLINES 5000
char *lineptr[MAXLINES];
int readlines(char *lineptr[], int nlines, char buf[]);
void writelines(char *lineptr[], int nlines);
void qsort(char *lineptr[], int left, int right);
#define MAXSIZE 10000
int main(int argc, char *argv[])
{
int nlines;
char buf[MAXSIZE];
if((nlines = readlines(lineptr, MAXLINES, buf)) >= 0) {
qsort(lineptr, 0, nlines-1);
writelines(lineptr, nlines);
getchar();
return 0;
}
else {
printf("error: input too big to sort\n");
getchar();
return 1;
}
}
#define MAXLEN 1000
int getline(char *, int);
int readlines(char *lineptr[], int maxlines, char buf[])
{
int len, nlines;
char *p = buf;
char line[MAXLEN];
nlines = 0;
while((len = getline(line, MAXLEN)) > 0)
if(nlines >= maxlines || MAXSIZE + buf - p < len)
return -1;
else {
line[len-1] = '\0';
strcpy(p, line);
lineptr[nlines++] = p;
p+=len;
}
return nlines;
}
void writelines(char *lineptr[], int nlines)
{
while(nlines-- > 0)
printf("%s\n", *lineptr++);
}
int getline(char s[], int lim)
{
int c, i;
for (i = 0; i < lim - 1 && (c = getchar()) != EOF && c != '\n'; i++)
s[i] = c;
if (c == '\n') {
s[i++] = c;
}
s[i] = '\0';
return i;
}
void swap(char *v[], int i, int j)
{
char *temp;
temp = v[i];
v[i] = v[j];
v[j] = temp;
}
void qsort(char *v[], int left, int right) {
int i, last;
if(left >= right)
return;
swap(v, left, (left+right)/2);
last = left;
for(i = left + 1; i <= right; i++)
if(strcmp(v[i], v[left]) < 0)
swap(v, ++last, i);
swap(v, left, last);
qsort(v, left, last-1);
qsort(v, last+1, right);
}
这是作者想要的,还是我做错了?
另外,测量时间差异,在主线开始时启动时钟是否正确,然后在读取线路结束后停止它,并比较所花费的时间?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXLINES 5000
char *lineptr[MAXLINES];
int readlines(char *lineptr[], int nlines);
void writelines(char *lineptr[], int nlines);
void qsort(char *lineptr[], int left, int right);
char *alloc(int n);
#define MAXSIZE 10000
int main(int argc, char *argv[])
{
int nlines;
if((nlines = readlines(lineptr, MAXLINES)) >= 0) {
qsort(lineptr, 0, nlines-1);
writelines(lineptr, nlines);
getchar();
return 0;
}
else {
printf("error: input too big to sort\n");
getchar();
return 1;
}
}
#define MAXLEN 1000
int getline(char *, int);
int readlines(char *lineptr[], int maxlines)
{
int len, nlines;
char *p;
char line[MAXLEN];
nlines = 0;
while((len = getline(line, MAXLEN)) > 0)
if(nlines >= maxlines || (p = alloc(len)) == NULL)
return -1;
else {
line[len-1] = '\0';
strcpy(p, line);
lineptr[nlines++] = p;
}
return nlines;
}
void writelines(char *lineptr[], int nlines)
{
while(nlines-- > 0)
printf("%s\n", *lineptr++);
}
#define MAXALLOC 5000
char allocbuf[MAXALLOC];
char *allocp = allocbuf;
char *alloc(int n)
{
if(allocbuf + MAXALLOC - allocp >= n) {
allocp += n;
return allocp - n;
}
else
return 0;
}
int getline(char s[], int lim)
{
int c, i;
for (i = 0; i < lim - 1 && (c = getchar()) != EOF && c != '\n'; i++)
s[i] = c;
if (c == '\n') {
s[i++] = c;
}
s[i] = '\0';
return i;
}
void swap(char *v[], int i, int j)
{
char *temp;
temp = v[i];
v[i] = v[j];
v[j] = temp;
}
void qsort(char *v[], int left, int right) {
int i, last;
if(left >= right)
return;
swap(v, left, (left+right)/2);
last = left;
for(i = left + 1; i <= right; i++)
if(strcmp(v[i], v[left]) < 0)
swap(v, ++last, i);
swap(v, left, last);
qsort(v, left, last-1);
qsort(v, last+1, right);
}
这是原始代码。
答案 0 :(得分:1)
很难说没有看到原始代码,但它看起来是正确的。
对于计时,它取决于您使用的时钟类型。如果您所做的只是从全局时钟读取时钟滴答,那么这种方法很糟糕。如果您正在读取进程的时钟,那么它将起作用。
编辑:你可能看不到很好的时间差异。但是,尝试将数组放在不同的函数中(让我们将它命名为foo())并使用foo()中的本地数组从该函数调用readlines。拨打foo()5000次。使用alloc()5000次尝试旧方法。
这可能会产生不同的运行时间,因为alloc()需要系统调用,这很慢。
编辑:这个伪c将告诉你如何计算动态内存分配与堆栈分配的时间。这很复杂,因为您的进程可能会阻塞,直到系统为其提供所需的内存,因此您可能需要修改不同的时钟以获得更好的图像。
你想要在没有优化的情况下编译它。
void call1()
{
char* i;
i = (char*)malloc(1000);
//we should free(), but this would add timing overhead.
}
void call2()
{
char x[1000];
}
int main()
{
int i=0;
timer_start();
for(i = 0; i < 5000; i++)
call1();
timer_stop();
report_time();
timer_start();
for(i = 0; i < 5000; i++)
call2();
timer_stop();
report_time();
}