You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am trying to optimize the Cosine method in my project. I use simsimd_cos_f32() to replace it and compare the time of the two. The following is the comparison code of the two solutions. You can see that the time is relatively close. Can someone tell me why, thanks a lot?
Steps to reproduce
code is here:
#include <chrono>
#include <iostream>
#include <random>
#include <vector>
#include <algorithm>
#include <simsimd/simsimd.h>
#include <immintrin.h>
using namespace std;
static float Cosine(const std::vector<float>& a, const std::vector<float>& b) {
float sum = 0.0f;
float norm1 = 0.0f;
float norm2 = 0.0f;
size_t dim = a.size();
for (size_t i = 0; i < dim; i++) {
float elem1 = a[i];
float elem2 = b[i];
sum += elem1 * elem2;
norm1 += elem1 * elem1;
norm2 += elem2 * elem2;
}
return sum / sqrt(norm1 * norm2);
}
static float SimdCosine2(const std::vector<float>& a,
const std::vector<float>& b) {
simsimd_distance_t distance;
// Cosine distance between two vectors
simsimd_cos_f32(a.data(), b.data(), a.size(), &distance);
return distance;
}
int main() {
{
// create Ramdon float value
size_t count = 100000;
size_t dim = 1000;
size_t times = 1000000;
vector<vector<float>> src(count);
std::mt19937 generator;
std::uniform_real_distribution<float> distribution(0, 1);
std::uniform_int_distribution<int> idx_distribution(0, count-1);
for (size_t i = 0; i < count; i++) {
vector<float> tmp(dim);
for (size_t d = 0; d < dim; d++) {
tmp[d] = distribution(generator);
}
src[i] = tmp;
}
vector<std::pair<int, int>> idx(times);
for (size_t i = 0; i < times; i++) {
idx[i] = {idx_distribution(generator), idx_distribution(generator)};
}
{
auto start = std::chrono::high_resolution_clock::now();
for (size_t i = 0; i < times; i++) {
Cosine(src[idx[i].first], src[idx[i].second]);
}
auto end = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> elapsed_seconds = end - start;
std::cout << "Cosine cost:"
<< std::chrono::duration_cast<std::chrono::milliseconds>(
end - start)
.count()
<< "ms.\n";
}
{
auto start = std::chrono::high_resolution_clock::now();
for (size_t i = 0; i < times; i++) {
SimdCosine2(src[idx[i].first], src[idx[i].second]);
}
auto end = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> elapsed_seconds = end - start;
std::cout << "SimdCosine2 cost:"
<< std::chrono::duration_cast<std::chrono::milliseconds>(
end - start)
.count()
<< "ms.\n";
}
}
}
Describe the bug
I am trying to optimize the Cosine method in my project. I use simsimd_cos_f32() to replace it and compare the time of the two. The following is the comparison code of the two solutions. You can see that the time is relatively close. Can someone tell me why, thanks a lot?
Steps to reproduce
code is here:
The compilation instructions are as follows
The execution results are as follows
Expected behavior
Expect simsimd_cos_f32 to be faster than the original solution
SimSIMD version
6.2.3
Operating System
CentOS Linux release 7.3.1611
Hardware architecture
x86
Which interface are you using?
C implementation
Contact Details
No response
Are you open to being tagged as a contributor?
.git
history as a contributorIs there an existing issue for this?
Code of Conduct
The text was updated successfully, but these errors were encountered: