2013年12月12日木曜日

開発環境

C実践プログラミング 第3版 (Steve Oualline (著)、 望月 康司 (監訳) (翻訳)、谷口 功 (翻訳)、オライリー・ジャパン)のⅡ部(単純なプログラミング)の15章(デバッグと最適化)、15.8(プログラミング実習)、実習 15-5を解いてみる。

その他参考書籍

実習15-5.

コード

sample.c

#include <stdio.h>
#include <memory.h>
#define X_SIZE 10000000
#define Y_SIZE 32

int matrix[X_SIZE][Y_SIZE];
int matrix1[X_SIZE][Y_SIZE];

int main(){
    void my_memcpy(void);
    memset(matrix, -1, sizeof(matrix));
    my_memcpy();
    return (0);
}

void my_memcpy()
{
    register int *matrix_ptr;
    register int *matrix1_ptr;
    for(matrix_ptr = &matrix[0][0], matrix1_ptr = &matrix1[0][0];
            matrix_ptr <= &matrix[X_SIZE - 1][Y_SIZE - 1];
            ++matrix_ptr, ++matrix1_ptr){
        *matrix1_ptr = *matrix_ptr;
    }
}

コード

sample1.c

#include <stdio.h>
#include <memory.h>
#define X_SIZE 10000000
#define Y_SIZE 32

int matrix[X_SIZE][Y_SIZE];
int matrix1[X_SIZE][Y_SIZE];

int main(){
    memset(matrix, -1, sizeof(matrix));
    memcpy(matrix1, matrix, sizeof(matrix));
    return (0);
}

makefile

CC=cc
CFLAGS=-g

sample: sample.c
 $(CC) $(CFLAGS) -o sample sample.c

clean:
 rm -f sample

入出力結果(Terminal)

$ make
cc -g -o sample sample.c
$ cc -g -o sample1 sample1.c
$ time ./sample

real 0m4.145s
user 0m1.693s
sys 0m2.398s
$ time ./sample1

real 0m3.611s
user 0m1.228s
sys 0m2.352s
$

0 コメント:

コメントを投稿