As known read operation from misaligned address slow than read operation from aligned address. But what about write data to a misaligned address?
Based on the results of the following tests, the performance is approximately the same
#include <iostream>
#include <chrono>
#include <limits>
int main()
{
char *number = new char[sizeof(unsigned int) + 1];
++number;//misaligned address
unsigned int *n = reinterpret_cast<unsigned int*>(number);
*n = 0;
unsigned int divisor;
std::cin >> divisor;
auto start = std::chrono::steady_clock::now();
for (unsigned int i = 0; i < std::numeric_limits<unsigned int>::max() / divisor; i++) {
*n = i;
}
auto end = std::chrono::steady_clock::now();
std::cout << *n << '\n';
std::cout << (end - start).count() << '\n';
}
But i am not sure the test is correct and result of writing data to misaligned and aligned address is same on processor level. Are there any performance issues in this case?