Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // SCSLab2.cpp : Defines the entry point for the console application.
- //
- #include "stdafx.h"
- #include "malloc.h"
- #define rdtsc __asm __emit 0fh __asm __emit 031h
- #define cpuid __asm __emit 0fh __asm __emit 0a2h
- int * generateArray(const int n){
- int array [100];
- for(int i=0; i<n; i++){
- array[i] = n-i;
- }
- return array;
- }
- int * generateArrayDynamic(int n){
- int ** array = (int **)malloc(n*sizeof(int));
- for(int i=0; i<n; i++){
- array[i] = (int *)malloc(sizeof(int));
- *array[i] = n-i;
- }
- return *array;
- }
- void sort(int array[], int n){
- for(int i =0; i<n; i++){
- for(int j = i; j<n; j++){
- if(array[i] > array[j]){
- array[i] = array[i] + array[j];
- array[j] = array[i] - array[j];
- array[i] = array[i] - array[j];
- }
- }
- }
- }
- void sortDynamic(int * array, int * next){
- if(array == NULL || next == NULL) return;
- else{
- if(*array > *next){
- int *aux = array;
- array = next;
- next = aux;
- }
- sortDynamic(array, next+1);
- sortDynamic(array+1, array+1);
- }
- }
- int main()
- {
- unsigned cycles_high1=0, cycles_low1=0, cupid_time=0;
- unsigned cycles_high2=0, cycles_low2=0;
- unsigned __int64 temp_cycles1=0, temp_cycles2=0;
- __int64 total_cycles=0;
- int var;
- //compute the CPUID overhead
- __asm {
- pushad
- CPUID
- RDTSC
- mov cycles_high1, edx
- mov cycles_low1, eax
- popad
- pushad
- CPUID
- RDTSC
- popad
- pushad
- CPUID
- RDTSC
- mov cycles_high1, edx
- mov cycles_low1, eax
- popad
- pushad
- CPUID
- RDTSC
- popad
- pushad
- CPUID
- RDTSC
- mov cycles_high1, edx
- mov cycles_low1, eax
- popad
- pushad
- CPUID
- RDTSC
- sub eax, cycles_low1
- mov cupid_time, eax
- popad
- pushad
- CPUID
- RDTSC
- mov cycles_high1, edx
- mov cycles_low1, eax
- popad
- pushad
- CPUID
- RDTSC
- sub eax, cycles_low1
- mov cupid_time, eax
- popad
- }
- cycles_high1=0;
- cycles_low1=0;
- int * array;
- int avr_times;
- //Measure the code sequence
- int var1, var2 = 5;
- __asm {
- pushad
- CPUID
- RDTSC
- mov cycles_high1, edx
- mov cycles_low1, eax
- popad
- }
- //Section of code to be measured
- /*__asm{
- add var2, ecx
- }
- __asm{
- add edx, ecx
- }
- __asm{
- mov edx, 9
- mul ecx
- }
- __asm{
- mov edx, 94
- mov ecx, 84
- fdiv
- }
- __asm{
- mov edx, 94
- mov ecx, 84
- fsub
- }*/
- //Sorts
- array=generateArray(90);
- sort(array, 90);
- __asm {
- pushad
- CPUID
- RDTSC
- mov cycles_high2, edx
- mov cycles_low2, eax
- popad
- }
- /*temp_cycles1 = ((unsigned __int64)cycles_high1 << 32) | cycles_low1;
- temp_cycles2 = ((unsigned __int64)cycles_high2 << 32) | cycles_low2;
- total_cycles = temp_cycles2 - temp_cycles1 - cupid_time;
- avr_times += total_cycles;*/
- /* Exact time
- array=generateArrayDynamic(90);
- sort(array, 90);
- */
- temp_cycles1 = ((unsigned __int64)cycles_high1 << 32) | cycles_low1;
- temp_cycles2 = ((unsigned __int64)cycles_high2 << 32) | cycles_low2;
- total_cycles = temp_cycles2 - temp_cycles1 - cupid_time;
- printf("total cycles: %d\n\r", total_cycles);
- printf("total seconds: %f\n\r", 1.0/total_cycles);
- getchar();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement