Skip to content

Instantly share code, notes, and snippets.

@pwq1989
Last active December 22, 2015 06:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pwq1989/6433168 to your computer and use it in GitHub Desktop.
Save pwq1989/6433168 to your computer and use it in GitHub Desktop.
A test program for this Question which faster in follows: 1. sort 10000 int 2. read 1M bytes from memory
#include <iostream>
#include<stdlib.h>
#include <stdio.h>
#include "stdlib.h"
#include "string.h"
using namespace std;
//void set_affinity();
#if defined(__i386__)
static __inline__ unsigned long long rdtsc(void)
{
unsigned long long int x;
__asm__ volatile (".byte 0x0f, 0x31" : "=A" (x));
return x;
}
#elif defined(__x86_64__)
static __inline__ unsigned long long rdtsc(void)
{
unsigned hi, lo;
__asm__ __volatile__ ("rdtsc" : "=a"(lo), "=d"(hi));
return ( (unsigned long long)lo)|( ((unsigned long long)hi)<<32 );
}
#endif
int comp(const void * a,const void * b)
{
return *(int32_t *)a - *(int32_t *)b;
}
void read_memory(char* start, int len)
{
for(int i=0; i<len; i += 4)
{
__asm__ ("movl %0, %%eax ;" : :"r"(start+i) :"%eax");
}
}
int main(void)
{
int bytes_size = 1024*1024;//1M bytes
int arr_len = 10000;
//set_affinity();
int32_t* arr = (int32_t*)malloc(sizeof(int32_t)*arr_len);
for(int i = 0; i < arr_len; i++) {
*(arr + i) = rand();
}
char* memory_block = (char*)malloc(sizeof(char)*bytes_size);
unsigned long long start;
unsigned long long end;
start = rdtsc();
qsort(arr,arr_len,sizeof(arr[0]),comp);
end = rdtsc();
printf("sort use time:%d\n",end - start);
start = rdtsc();
read_memory(memory_block, bytes_size);
end = rdtsc();
printf("read memory use time:%d\n",end - start);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment