Skip to content

Commit

Permalink
Create basic benchmark #7
Browse files Browse the repository at this point in the history
  • Loading branch information
jurihock committed Jun 19, 2023
1 parent 4497596 commit 3cbec51
Show file tree
Hide file tree
Showing 7 changed files with 196 additions and 9 deletions.
14 changes: 13 additions & 1 deletion cpp/Makefile
Original file line number Diff line number Diff line change
@@ -1,12 +1,24 @@
.PHONY: help build clean
.PHONY: help build clean bench analysis

help:
@echo build
@echo clean
@echo bench
@echo analysis

build:
@cmake -S . -B build
@cmake --build build

clean:
@rm -rf build

bench:
@cmake -DCMAKE_BUILD_TYPE=Release -S . -B build-bench
@cmake --build build-bench
@build-bench/qdft-example-bench

analysis:
@cmake -S . -B build
@cmake --build build
@build/qdft-example-analysis
17 changes: 17 additions & 0 deletions cpp/examples/bench.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
project(qdft-example-bench)

add_executable(${PROJECT_NAME})

target_sources(${PROJECT_NAME}
PRIVATE "${CMAKE_CURRENT_LIST_DIR}/bench.cpp")

target_link_libraries(${PROJECT_NAME}
PRIVATE qdft::cpp)

if (MSVC)
target_compile_options(${PROJECT_NAME}
PRIVATE /fp:fast)
else()
target_compile_options(${PROJECT_NAME}
PRIVATE -ffast-math)
endif()
54 changes: 54 additions & 0 deletions cpp/examples/bench.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#include <qdft/qdft.h>

#include <algorithm>
#include <chrono>
#include <complex>
#include <iostream>
#include <vector>

int main()
{
const auto samplerate = 44100;
const auto bandwidth = std::make_pair(50.0, samplerate / 2.0);
const auto resolution = 24;
const auto latency = 0;
const auto window = std::make_pair(+0.5, -0.5);

const auto ta0 = std::chrono::high_resolution_clock::now();
QDFT qdft(samplerate, bandwidth, resolution, latency, window);
const auto tb0 = std::chrono::high_resolution_clock::now();
const auto e0 = std::chrono::duration_cast<std::chrono::microseconds>(tb0 - ta0).count();

std::cout << "PREP\t" << "CPP\t" << e0 << " us" << std::endl;

const auto n = 1 * samplerate;
const auto m = qdft.size();

std::vector<float> x(n);
std::vector<std::complex<double>> y(n * m);

const auto runs = 10;

for (auto run = 1; run <= runs; ++run)
{
std::cout << "RUN\t" << run << "/" << runs << std::endl;

std::fill(x.begin(), x.end(), 0);
std::fill(y.begin(), y.end(), 0);

const auto ta1 = std::chrono::high_resolution_clock::now();
qdft.qdft(n, x.data(), y.data());
const auto tb1 = std::chrono::high_resolution_clock::now();
const auto e1 = std::chrono::duration_cast<std::chrono::microseconds>(tb1 - ta1).count();

const auto ta2 = std::chrono::high_resolution_clock::now();
qdft.iqdft(n, y.data(), x.data());
const auto tb2 = std::chrono::high_resolution_clock::now();
const auto e2 = std::chrono::duration_cast<std::chrono::microseconds>(tb2 - ta2).count();

std::cout << "\tQDFT\t" << e1 << " us" << std::endl;
std::cout << "\tIQDFT\t" << e2 << " us" << std::endl;
}

return 0;
}
45 changes: 45 additions & 0 deletions python/examples/bench.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import os, sys
src = os.path.join(os.path.dirname(__file__), '..', 'src')
sys.path.insert(0, src)

from qdft import QDFT
from timeit import default_timer as timer
import numpy as np

samplerate = 44100
bandwidth = (50, samplerate / 2)
resolution = 24
latency = 0
window = (+0.5, -0.5)

ta0 = timer()
qdft = QDFT(samplerate, bandwidth, resolution, latency, window)
tb0 = timer()
e0 = int((tb0 - ta0) * 1e+6)

print(f"PREP\tPYTHON\t{e0} us")

n = 1 * samplerate

x = np.ndarray(n, dtype=float)

runs = 10

for run in range(1, runs + 1):

print(f"RUN\t{run}/{runs}")

x[:] = 0

ta1 = timer()
y = qdft.qdft(x)
tb1 = timer()
e1 = int((tb1 - ta1) * 1e+6)

ta2 = timer()
x = qdft.iqdft(y)
tb2 = timer()
e2 = int((tb2 - ta2) * 1e+6)

print(f"\tQDFT\t{e1} us")
print(f"\tIQDFT\t{e2} us")
2 changes: 1 addition & 1 deletion rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ keywords = ["cqt", "constant-q", "sliding", "dft", "discrete", "fourier", "trans
categories = ["algorithms", "mathematics", "multimedia", "science"]

[dependencies]
num = "*"
num = "0.4.*"

[dev-dependencies]
ndarray = "0.15.*"
Expand Down
21 changes: 14 additions & 7 deletions rust/Makefile
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@
.PHONY: help build clean example test
.PHONY: help build clean test bench analysis

help:
@echo build
@echo clean
@echo test
@echo bench
@echo analysis

build:
@cargo build
@cargo build --workspace

clean:
@rm -rf target

example:
@cargo build --examples
@cargo run --example analysis
@python examples/analysis.py

test:
@cargo build --tests
@cargo test --workspace

bench:
@cargo build --release --example bench
@cargo run --release --example bench

analysis:
@cargo build --example analysis
@cargo run --example analysis
@python examples/analysis.py
52 changes: 52 additions & 0 deletions rust/examples/bench.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
use qdft::QDFT;

use num::Zero;
use std::time::Instant;

#[allow(non_camel_case_types)]
type c64 = num::complex::Complex<f64>;

fn main() {
let samplerate = 44100.0;
let bandwidth = (50.0, samplerate / 2.0);
let resolution = 24.0;
let latency = 0.0;
let window = Some((0.5, -0.5));

let t0 = Instant::now();
let mut qdft = QDFT::new(
samplerate,
bandwidth,
resolution,
latency,
window);
let e0 = t0.elapsed();

println!("PREP\tRUST\t{} us", e0.as_micros());

let n = 1 * samplerate as usize;
let m = qdft.size();

let mut x = vec![f32::zero(); n];
let mut y = vec![c64::zero(); n * m];

let runs = 10;

for run in 1 .. runs + 1 {
println!("RUN\t{}/{}", run, runs);

x.fill(f32::zero());
y.fill(c64::zero());

let t1 = Instant::now();
qdft.qdft(&x, &mut y);
let e1 = t1.elapsed();

let t2 = Instant::now();
qdft.iqdft(&y, &mut x);
let e2 = t2.elapsed();

println!("\tQDFT\t{} us", e1.as_micros());
println!("\tIQDFT\t{} us", e2.as_micros());
}
}

0 comments on commit 3cbec51

Please sign in to comment.