forked from aravind-kumar/fb-folly
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lazy_eval.cpp
63 lines (45 loc) · 1.39 KB
/
lazy_eval.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <iostream>
#include <vector>
#include <sys/time.h>
#include <ctime>
#include <folly/Lazy.h>
typedef long long int64;
typedef unsigned long long uint64;
/* Results:
Note: Based on a few runs only on my mac os
Time to initilize lazy = 0 ms
Time after first use 193 ms
Time withourLazy 185 ms
*/
uint64 GetTimeMs64()
{
struct timeval tv;
gettimeofday(&tv, NULL);
uint64 ret = tv.tv_usec;
/* Convert from micro seconds (10^-6) to milliseconds (10^-3) */
ret /= 1000;
/* Adds the seconds (10^0) after converting them to milliseconds (10^-3) */
ret += (tv.tv_sec * 1000);
return ret;
}
int main()
{
/////////////////////////////////////////////////////////////////////////
uint64 start = GetTimeMs64();
auto lazyVector = folly::lazy
([]{ return std::vector<int>(9999999,-1);});
uint64 end = GetTimeMs64();
/////////////////////////////////////////////////////////////////////////
std::cout<<"\n Time to initilize lazy = "<<end-start<<" ms";
start = GetTimeMs64();
lazyVector().push_back(29);
end = GetTimeMs64();
std::cout<<"\n Time after first use "<<end-start<<" ms";
/////////////////////////////////////////////////////////////////////////
start = GetTimeMs64();
std::vector<int> withouLazy(9999999,-1);
withouLazy.push_back(29);
end = GetTimeMs64();
std::cout<<"\n Time withourLazy "<<end-start<<" ms";
return 0;
}