Skip to content

Instantly share code, notes, and snippets.

@ykm11
Last active February 23, 2024 05:55
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 ykm11/1a338f6a56986ea166ba69adb39973e7 to your computer and use it in GitHub Desktop.
Save ykm11/1a338f6a56986ea166ba69adb39973e7 to your computer and use it in GitHub Desktop.
mpf_t init benchmark
#include <iostream>
#include <gmpxx.h>
#include <cstdint>
#include <chrono>
#include <vector>
#include <stdlib.h>
#define PREC 64*4
#define PREC_LIMB_SIZE 4
const size_t N = 100;
const size_t sample = 156250;
void bench1() {
//mpf_t a[sample], b[sample], c[sample];
//mp_limb_t a_mp_d[(PREC_LIMB_SIZE + 1) * sample];
//mp_limb_t b_mp_d[(PREC_LIMB_SIZE + 1) * sample];
//mp_limb_t c_mp_d[(PREC_LIMB_SIZE + 1) * sample];
mpf_t *a = (mpf_t *)malloc(sizeof(mpf_t) * sample);
mpf_t *b = (mpf_t *)malloc(sizeof(mpf_t) * sample);
mpf_t *c = (mpf_t *)malloc(sizeof(mpf_t) * sample);
mp_limb_t *a_mp_d = (mp_limb_t *)malloc(sizeof(mp_limb_t)*
(PREC_LIMB_SIZE + 1) * sample);
mp_limb_t *b_mp_d = (mp_limb_t *)malloc(sizeof(mp_limb_t)*
(PREC_LIMB_SIZE + 1) * sample);
mp_limb_t *c_mp_d = (mp_limb_t *)malloc(sizeof(mp_limb_t)*
(PREC_LIMB_SIZE + 1) * sample);
for (size_t i = 0; i < sample; i++) {
a[i]->_mp_d = a_mp_d + (PREC_LIMB_SIZE + 1) * i;
b[i]->_mp_d = b_mp_d + (PREC_LIMB_SIZE + 1) * i;
c[i]->_mp_d = c_mp_d + (PREC_LIMB_SIZE + 1) * i;
mpf_set_d(a[i], i * 81.01919);
mpf_set_d(b[i], i * 11.4514);
//mpf_set_d(c[i], 0);
a[i]->_mp_prec = PREC_LIMB_SIZE;
b[i]->_mp_prec = PREC_LIMB_SIZE;
c[i]->_mp_prec = PREC_LIMB_SIZE;
}
for (size_t i = 0; i < sample; i++) {
mpf_mul(c[i], a[i], b[i]);
}
//gmp_printf("%.16Ff\n", a[1]);
//gmp_printf("%.16Ff\n", b[1]);
//gmp_printf("%.16Ff\n", c[1]);
free(a);
free(b);
free(c);
free(a_mp_d);
free(b_mp_d);
free(c_mp_d);
}
void bench2() {
//mpf_t a[sample], b[sample], c[sample];
mpf_t *a = (mpf_t *)malloc(sizeof(mpf_t) * sample);
mpf_t *b = (mpf_t *)malloc(sizeof(mpf_t) * sample);
mpf_t *c = (mpf_t *)malloc(sizeof(mpf_t) * sample);
for (size_t i = 0; i < sample; i++) {
mpf_init2(a[i], PREC);
mpf_init2(b[i], PREC);
mpf_init2(c[i], PREC);
mpf_set_d(a[i], i * 81.01919);
mpf_set_d(b[i], i * 11.4514);
}
for (size_t i = 0; i < sample; i++) {
mpf_mul(c[i], a[i], b[i]);
}
//gmp_printf("%.16Ff\n", a[1]);
//gmp_printf("%.16Ff\n", b[1]);
//gmp_printf("%.16Ff\n", c[1]);
for (size_t i = 0; i < sample; i++) {
mpf_clear(a[i]);
mpf_clear(b[i]);
mpf_clear(c[i]);
}
free(a);
free(b);
free(c);
}
void bench3(size_t n) {
//mpf_t a[sample], b[sample], c[sample];
//mp_limb_t a_mp_d[(PREC_LIMB_SIZE + 1) * sample];
//mp_limb_t b_mp_d[(PREC_LIMB_SIZE + 1) * sample];
//mp_limb_t c_mp_d[(PREC_LIMB_SIZE + 1) * sample];
mpf_t *a = (mpf_t *)malloc(sizeof(mpf_t) * n);
mpf_t *b = (mpf_t *)malloc(sizeof(mpf_t) * n);
mpf_t *c = (mpf_t *)malloc(sizeof(mpf_t) * n);
mp_limb_t *a_mp_d = (mp_limb_t *)malloc(sizeof(mp_limb_t)*
(PREC_LIMB_SIZE + 1) * n);
mp_limb_t *b_mp_d = (mp_limb_t *)malloc(sizeof(mp_limb_t)*
(PREC_LIMB_SIZE + 1) * n);
mp_limb_t *c_mp_d = (mp_limb_t *)malloc(sizeof(mp_limb_t)*
(PREC_LIMB_SIZE + 1) * n);
for (size_t i = 0; i < sample; i++) {
a[i]->_mp_d = a_mp_d + (PREC_LIMB_SIZE + 1) * i;
b[i]->_mp_d = b_mp_d + (PREC_LIMB_SIZE + 1) * i;
c[i]->_mp_d = c_mp_d + (PREC_LIMB_SIZE + 1) * i;
mpf_set_d(a[i], i * 81.01919);
mpf_set_d(b[i], i * 11.4514);
//mpf_set_d(c[i], 0);
a[i]->_mp_prec = PREC_LIMB_SIZE;
b[i]->_mp_prec = PREC_LIMB_SIZE;
c[i]->_mp_prec = PREC_LIMB_SIZE;
}
for (size_t i = 0; i < sample; i++) {
mpf_mul(c[i], a[i], b[i]);
}
//gmp_printf("%.16Ff\n", a[1]);
//gmp_printf("%.16Ff\n", b[1]);
//gmp_printf("%.16Ff\n", c[1]);
free(a);
free(b);
free(c);
free(a_mp_d);
free(b_mp_d);
free(c_mp_d);
}
void bench4() {
//mpf_class a[sample], b[sample], c[sample];
std::vector<mpf_class> a(sample);
std::vector<mpf_class> b(sample);
std::vector<mpf_class> c(sample);
for (size_t i = 0; i < sample; i++) {
a[i] = i * 81.01919;
b[i] = i * 11.4514;
//mpf_set_d(a[i], i * 81.01919);
//mpf_set_d(b[i], i * 11.4514);
}
for (size_t i = 0; i < sample; i++) {
mpf_mul(c[i].get_mpf_t(), a[i].get_mpf_t(), b[i].get_mpf_t());
}
//gmp_printf("%.16Ff\n", a[1]);
//gmp_printf("%.16Ff\n", b[1]);
//gmp_printf("%.16Ff\n", c[1]);
}
int main() {
auto start_time = std::chrono::high_resolution_clock::now();
for (size_t i = 0; i < N; i++) {
bench1();
}
auto end_time = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::microseconds>(end_time - start_time);
std::cerr << "\tExecution Time: " << (duration.count() / double(N) / 1.0e6)
<< " seconds" << std::endl;
start_time = std::chrono::high_resolution_clock::now();
for (size_t i = 0; i < N; i++) {
bench2();
}
end_time = std::chrono::high_resolution_clock::now();
duration = std::chrono::duration_cast<std::chrono::microseconds>(end_time -
start_time);
std::cerr << "\tExecution Time: " << (duration.count() / double(N) / 1.0e6)
<< " seconds" << std::endl;
start_time = std::chrono::high_resolution_clock::now();
for (size_t i = 0; i < N; i++) {
bench3(sample);
}
end_time = std::chrono::high_resolution_clock::now();
duration = std::chrono::duration_cast<std::chrono::microseconds>(end_time -
start_time);
std::cerr << "\tExecution Time: " << (duration.count() / double(N) / 1.0e6)
<< " seconds" << std::endl;
mpf_set_default_prec(PREC);
start_time = std::chrono::high_resolution_clock::now();
for (size_t i = 0; i < N; i++) {
bench4();
}
end_time = std::chrono::high_resolution_clock::now();
duration = std::chrono::duration_cast<std::chrono::microseconds>(end_time -
start_time);
std::cerr << "\tExecution Time: " << (duration.count() / double(N) / 1.0e6)
<< " seconds" << std::endl;
}
ykm11: $ g++ piyo.cpp -lgmpxx -lgmp -O3
ykm11: $ ./a.out
Execution Time: 0.0101858 seconds
Execution Time: 0.0294115 seconds
Execution Time: 0.00777957 seconds
Execution Time: 0.0221703 seconds
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment