資源簡介
CSAPP性能優化實驗,我這里做了三個優化,供大家參考。里面還有一個網上下載的PPT講解,提供給大家學習

代碼片段和文件信息
#include?
#include?
#include?
#include?
#include?“clock.h“
/*?
?*?Routines?for?using?the?cycle?counter?
?*/
/*?Detect?whether?running?on?Alpha?*/
#ifdef?__alpha
#define?IS_ALPHA?1
#else
#define?IS_ALPHA?0
#endif
/*?Detect?whether?running?on?x86?*/
#ifdef?__i386__
#define?IS_x86?1
#else
#define?IS_x86?0
#endif
#if?IS_ALPHA
/*?Initialize?the?cycle?counter?*/
static?unsigned?cyc_hi?=?0;
static?unsigned?cyc_lo?=?0;
/*?Use?Alpha?cycle?timer?to?compute?cycles.??Then?use
???measured?clock?speed?to?compute?seconds?
*/
/*
?*?counterRoutine?is?an?array?of?Alpha?instructions?to?access?
?*?the?Alpha‘s?processor?cycle?counter.?It?uses?the?rpcc?
?*?instruction?to?access?the?counter.?This?64?bit?register?is?
?*?divided?into?two?parts.?The?lower?32?bits?are?the?cycles?
?*?used?by?the?current?process.?The?upper?32?bits?are?wall?
?*?clock?cycles.?These?instructions?read?the?counter?and?
?*?convert?the?lower?32?bits?into?an?unsigned?int?-?this?is?the?
?*?user?space?counter?value.
?*?NOTE:?The?counter?has?a?very?limited?time?span.?With?a?
?*?450MhZ?clock?the?counter?can?time?things?for?about?9?
?*?seconds.?*/
static?unsigned?int?counterRoutine[]?=
{
????0x601fc000u
????0x401f0000u
????0x6bfa8001u
};
/*?Cast?the?above?instructions?into?a?function.?*/
static?unsigned?int?(*counter)(void)=?(void?*)counterRoutine;
void?start_counter()
{
????/*?Get?cycle?counter?*/
????cyc_hi?=?0;
????cyc_lo?=?counter();
}
double?get_counter()
{
????unsigned?ncyc_hi?ncyc_lo;
????unsigned?hi?lo?borrow;
????double?result;
????ncyc_lo?=?counter();
????ncyc_hi?=?0;
????lo?=?ncyc_lo?-?cyc_lo;
????borrow?=?lo?>?ncyc_lo;
????hi?=?ncyc_hi?-?cyc_hi?-?borrow;
????result?=?(double)?hi?*?(1?<30)?*?4?+?lo;
????if?(result?0)?{
fprintf(stderr?“Error:?Cycle?counter?returning?negative?value:?%.0f\n“?result);
????}
????return?result;
}
#endif?/*?Alpha?*/
#if?IS_x86
/*?$begin?x86cyclecounter?*/
/*?Initialize?the?cycle?counter?*/
static?unsigned?cyc_hi?=?0;
static?unsigned?cyc_lo?=?0;
/*?Set?*hi?and?*lo?to?the?high?and?low?order?bits??of?the?cycle?counter.??
???Implementation?requires?assembly?code?to?use?the?rdtsc?instruction.?*/
void?access_counter(unsigned?*hi?unsigned?*lo)
{
????asm(“rdtsc;?movl?%%edx%0;?movl?%%eax%1“???/*?Read?cycle?counter?*/
:?“=r“?(*hi)?“=r“?(*lo)????????????????/*?and?move?results?to?*/
:?/*?No?input?*/????????????????????????/*?the?two?outputs?*/
:?“%edx“?“%eax“);
}
/*?Record?the?current?value?of?the?cycle?counter.?*/
void?start_counter()
{
????access_counter(&cyc_hi?&cyc_lo);
}
/*?Return?the?number?of?cycles?since?the?last?call?to?start_counter.?*/
double?get_counter()
{
????unsigned?ncyc_hi?ncyc_lo;
????unsigned?hi?lo?borrow;
????double?result;
????/*?Get?cycle?counter?*/
????access_counter(&ncyc_hi?&ncyc_lo);
????/*?Do?double?precision?subtraction?*/
????lo?=?ncyc_lo?-?cyc_lo;
????borrow?=?lo?>?ncyc_lo;
????hi?=?ncyc_hi?-?cyc_hi?-?borrow;
????result?=?(double)?hi?*?(1?<30)?*?4?+?lo;
????if?(
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件???????5459??2015-05-15?21:00??LAB4\clock.c
?????文件????????542??2015-05-15?21:00??LAB4\clock.h
?????文件????????877??2015-05-15?21:00??LAB4\config.h
?????文件????????733??2015-05-15?21:00??LAB4\defs.h
?????文件??????19839??2015-05-15?21:00??LAB4\driver.c
?????文件???????5237??2015-05-15?21:00??LAB4\fcyc.c
?????文件???????1340??2015-05-15?21:00??LAB4\fcyc.h
?????文件???????5346??2015-05-15?21:00??LAB4\kernels.c
?????文件?????611693??2015-07-11?15:24??LAB4\LAB4.docx
?????文件????????392??2015-05-15?21:00??LAB4\Makefile
?????文件???????9229??2015-05-15?23:04??LAB4\New?Text?Document.txt
?????文件??????51200??2015-05-15?21:00??LAB4\perflab-handout.tar
?????文件???????1140??2015-05-15?21:00??LAB4\README
?????文件???????1120??2015-05-15?21:00??LAB4\Readme?First.txt
?????文件????????592??2015-05-16?21:34??LAB4\優化代碼1.txt
?????文件?????318464??2015-05-16?20:28??LAB4\性能優化實驗分析.ppt
?????目錄??????????0??2015-07-11?15:24??LAB4
-----------?---------??----------?-----??----
??????????????1033203????????????????????17
評論
共有 條評論