Skip to content

Instantly share code, notes, and snippets.

@doitian
Created June 5, 2010 02:07
Show Gist options
  • Select an option

  • Save doitian/426215 to your computer and use it in GitHub Desktop.

Select an option

Save doitian/426215 to your computer and use it in GitHub Desktop.
CC = g++
LD = $(CC)
CC_FLAGS = -O2 -g0 -Wall
LD_FLAGS =
SRC = $(wildcard *.cpp)
OBJ = $(SRC:.cpp=.o)
BIN = $(SRC:.cpp=)
all: plot
build: $(BIN)
$(OBJ): %.o: %.cpp
$(CC) $(CC_FLAGS) -o $@ -c $<
$(BIN): %: %.o
$(LD) $(LD_FLAGS) -o $@ $<
run: time.dat time2.dat
time.dat: sum run.sh
./run.sh
time2.dat: sum run2.sh
./run2.sh
plot: time.png time2.png
time.png: time.gplot time.dat time2.dat
gnuplot time.gplot
time2.png: time2.gplot time.dat time2.dat
gnuplot time2.gplot
clean-images:
rm -f time.png time2.png
clean: clean-images
rm -f $(BIN) $(OBJ) *.dat
.PHONY: all build run plot clean-images clean
#!/bin/bash
: > time.dat
for i in `seq 0 20`; do
length=$((2**i))
./sum $length | head -1 >> time.dat
done
#!/bin/bash
: > time2.dat
for i in `seq 1 1024`; do
./sum $i | head -1 >> time2.dat
done
/**
* @file sum.cpp
* @author Ian Yang
* @date Created <2010-06-05 08:18:04>
*/
#include <iostream>
#include <ctime>
#include <cstdlib>
class Timer
{
std::clock_t start_;
public:
Timer()
: start_(std::clock())
{}
double elapsed() const
{
return double(std::clock() - start_) / CLOCKS_PER_SEC;
}
};
const std::size_t kTotalCount = 1 << 24;
void runTest(std::size_t slideLength);
int main(int argc, char *argv[])
{
if (argc != 2)
{
std::cerr << "Usage: sum slideLength" << std::endl;
return 1;
}
std::size_t slideLength = 0;
slideLength = std::atol(argv[1]);
if (slideLength == 0 || slideLength > kTotalCount)
{
std::cerr << "Invalid slideLength: " << argv[1] << std::endl;
return 2;
}
runTest(slideLength);
return 0;
}
void runTest(std::size_t slideLength)
{
double* elements = new double[kTotalCount];
for (std::size_t i = 0; i < kTotalCount; ++i)
{
elements[i] = i;
}
Timer timer;
double sum = 0.0;
std::size_t slideStep = kTotalCount / slideLength;
static const std::size_t kRepeat = 1;
for (std::size_t loopId = 0; loopId < kRepeat; ++loopId)
{
for (std::size_t outerLoopId = 0;
outerLoopId < slideStep; ++outerLoopId)
{
for (std::size_t index = outerLoopId;
index < kTotalCount; index += slideStep)
{
sum += elements[index];
}
}
}
std::cout.setf(std::ios::fixed);
std::cout.precision(8);
std::cout << slideLength << " " << timer.elapsed() << std::endl;
std::cout << sum << std::endl; // use the sum to avoid optimization
}
set terminal png font "DroidSansMono"
set output "time.png"
set logscale x 2
set xtics 2 rotate
set grid x y
set style data linespoints
set xlabel "slide length"
set ylabel "time (s)"
plot "time.dat", "time2.dat"
set terminal png font "DroidSansMono" size 4000,600
set output "time2.png"
set logscale x 2
set xtics 2 rotate
set grid x y
set style data linespoints
set xlabel "slide length"
set ylabel "time (s)"
plot [x=1:1024] "time.dat", "time2.dat"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment