C Why are vectors faster in FreeBSD?

Hi,

When I run the program using method1() in FreeBSD and Windows (visual studio), they both finished within 8 seconds.

But if I use method2(), Windows finished within 8 seconds and FreeBSD 1 sec. Can someone explain me why vectors are faster in FreeBSD?

Code:
#include <stdio.h>
#include <iostream>
#include <vector>

using namespace std;
void method1()
{
   for (int i = 0; i < 2000000000; i++);
   for (int i = 0; i < 2000000000; i++);
}
void method2()
{
   int number = 2;
   vector<int> row(1024,number);
   vector<vector<int>> rows(768, row);

   for (int i = 0; i < 7864320; i++)
   {
     rows[0][0] = number;
   }
   
}

int main(int argc, char **argv)
{    
   method1();
   //method2()
   cout<<"end"<<endl;
   return 0;
}
 
There is no simple answer. You have to profile both Windows and FreeBSD versions and see where and how much time is spent in method2(). It of course could prove that FreeBSD is doing something right but with so little data it's hard to say anything conclusive.
 
It's been a long time since I've dug deep into such workings but, on Quora, someone brought up a similar question. The answer was simple. Every task on Windows has some baggage and housekeeping involved in executing it that no other similar operating system has.
 
It's been a long time since I've dug deep into such workings but, on Quora, someone brought up a similar question. The answer was simple. Every task on Windows has some baggage and housekeeping involved in executing it that no other similar operating system has.

So does FreeBSD, it's called runtime environment. You can't jump to such conclusions without actually going into the nuts and bolts of what is executed on the CPU cores at the machine language level.
 
There's one fairly basic difference that might be it. The second method may be optimized differently by the compilers.

And if I read this correctly there could also be implementation differences. Take special note of this post:
In Visual C++, #define _SECURE_SCL 0 reduces UseVector by half (bringing it down to 4 seconds). This is really huge, IMO.
 
Your method2() is going to be optimized out of existence, basicly.

The expression rows[0][0] = number; is loop invariant, so it will be pulled out of the loop. After that, the loop is empty, the compiler is free to delete it. Your vectors are unused, so the aforementioned expression is a dead store, and is deleted. When using "cc -O2 ...", I would be surprised if that code contains anything but a return instruction. The same goes for method1(), by the way.

Edit: Okay, now I must admit to astonishment. A small test compile shows that clang throws away the code in method1(), as expected, but does not throw away method2(). I can only think of the allocator code being the hook here (it may change the global state when not called/called different number of times...). The vectors are constructed correctly. You are aware that for each entry in "rows" a new vector is allocated, you are calling the allocator/deallocator 768 times here? And then destructed again, also in an implicit loop. Maybe you should rethink this part a bit?
 
So does FreeBSD, it's called runtime environment. You can't jump to such conclusions without actually going into the nuts and bolts of what is executed on the CPU cores at the machine language level.
Yes, and that's what the posts, by members of CERN, did and showed.
 
Back
Top