SHARE
TWEET

Untitled

a guest Sep 29th, 2015 70 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Code here:
  2. Results below:
  3. #include <string>
  4. #include <list>
  5. #include <utility>
  6.  
  7. #define PTR
  8.  
  9.  
  10. // This is purposefully badly packed
  11. struct ExpensiveType
  12. {
  13.     int heapDataSize;
  14.     bool trueOrFalse1;
  15.     double loadsOfData[1024];
  16.     char* heapData;
  17.     bool trueOrFalse2;
  18.     std::string iHaveACtor;
  19.     std::list<std::string> expensive;
  20. };
  21.  
  22. // The way I would classically do it - I don't use non-const refs in
  23. // functions like this since I often forget that they can modify the
  24. // object if I don't look explicitly at the function signature
  25. void DoSomething(ExpensiveType* inOut)
  26. {
  27.     std::swap(inOut->trueOrFalse1, inOut->trueOrFalse2);
  28.     for (auto& i : inOut->loadsOfData)
  29.         i /= 2.0;
  30.  
  31.     inOut->iHaveACtor += "  argh this should probably require the string to allocate more memory";
  32.  
  33.     if (inOut->heapData != nullptr)
  34.     {
  35.         char* temp = new char[inOut->heapDataSize];
  36.         std::copy(inOut->heapData, inOut->heapData+inOut->heapDataSize, temp);
  37.         delete [] inOut->heapData;
  38.         inOut->heapData = temp;
  39.     }
  40.  
  41.     auto tempFront = inOut->expensive.front();
  42.     auto tempBack = inOut->expensive.back();
  43.     inOut->expensive.pop_front();
  44.     inOut->expensive.pop_back();
  45.     inOut->expensive.push_front(tempBack);
  46.     inOut->expensive.push_back(tempFront);
  47. }
  48.  
  49. // Using pass-by-value
  50. ExpensiveType DoSomethingElse(ExpensiveType in)
  51. {
  52.     std::swap(in.trueOrFalse1, in.trueOrFalse2);
  53.     for (auto& i : in.loadsOfData)
  54.         i /= 2.0;
  55.  
  56.     in.iHaveACtor += "  argh this should probably require the string to allocate more memory";
  57.  
  58.     if (in.heapData != nullptr)
  59.     {
  60.         char* temp = new char[in.heapDataSize];
  61.         std::copy(in.heapData, in.heapData+in.heapDataSize, temp);
  62.         delete [] in.heapData;
  63.         in.heapData = temp;
  64.     }
  65.  
  66.     auto tempFront = in.expensive.front();
  67.     auto tempBack = in.expensive.back();
  68.     in.expensive.pop_front();
  69.     in.expensive.pop_back();
  70.     in.expensive.push_front(tempBack);
  71.     in.expensive.push_back(tempFront);
  72.  
  73.     return in;
  74. }
  75.  
  76. int main(void)
  77. {
  78.     ExpensiveType data;
  79.     data.trueOrFalse1 = true;
  80.     data.trueOrFalse2 = false;
  81.     for (int i = 0; i < 1024; ++i)
  82.     {
  83.         data.loadsOfData[i] = i;
  84.         data.expensive.push_back(std::to_string(i));
  85.     }
  86.     data.heapDataSize = 10;
  87.     data.heapData = new char[data.heapDataSize];
  88.     for (int i = 0; i < data.heapDataSize; ++i)
  89.         data.heapData[i] = data.heapDataSize - i;
  90.  
  91.     for (int i = 0; i < 10e3; ++i)
  92.     {
  93. #ifdef PTR
  94.         DoSomething(&data);
  95. #else
  96.         data = DoSomethingElse(data);
  97. #endif
  98.         // try and make sure the loop doesn't get optimised out
  99.         data.trueOrFalse1 = !data.trueOrFalse1;
  100.     }
  101.  
  102. #ifdef PTR
  103.         DoSomething(&data);
  104. #else
  105.         data = DoSomethingElse(data);
  106. #endif
  107.  
  108.         return 0;
  109. }
  110.  
  111.  
  112. # Unoptimised Clang 3.7 (-O0)
  113. ## Value
  114. $ for i in {1..10}; do time bin/test; done
  115.  
  116. real    0m1.259s
  117. user    0m1.110s
  118. sys     0m0.143s
  119.  
  120. real    0m1.260s
  121. user    0m1.138s
  122. sys     0m0.116s
  123.  
  124. real    0m1.251s
  125. user    0m1.163s
  126. sys     0m0.088s
  127.  
  128. real    0m1.252s
  129. user    0m1.119s
  130. sys     0m0.128s
  131.  
  132. real    0m1.258s
  133. user    0m1.178s
  134. sys     0m0.080s
  135.  
  136. real    0m1.260s
  137. user    0m1.103s
  138. sys     0m0.152s
  139.  
  140. real    0m1.254s
  141. user    0m1.125s
  142. sys     0m0.124s
  143.  
  144. real    0m1.254s
  145. user    0m1.149s
  146. sys     0m0.099s
  147.  
  148. real    0m1.254s
  149. user    0m1.145s
  150. sys     0m0.104s
  151.  
  152. real    0m1.258s
  153. user    0m1.140s
  154. sys     0m0.112s
  155.  
  156. ## Pointer
  157. $ for i in {1..10}; do time bin/test; done
  158.  
  159. real    0m0.033s
  160. user    0m0.030s
  161. sys     0m0.004s
  162.  
  163. real    0m0.033s
  164. user    0m0.033s
  165. sys     0m0.000s
  166.  
  167. real    0m0.033s
  168. user    0m0.033s
  169. sys     0m0.000s
  170.  
  171. real    0m0.033s
  172. user    0m0.029s
  173. sys     0m0.004s
  174.  
  175. real    0m0.033s
  176. user    0m0.033s
  177. sys     0m0.000s
  178.  
  179. real    0m0.034s
  180. user    0m0.034s
  181. sys     0m0.000s
  182.  
  183. real    0m0.033s
  184. user    0m0.033s
  185. sys     0m0.005s
  186.  
  187. real    0m0.033s
  188. user    0m0.033s
  189. sys     0m0.000s
  190.  
  191. real    0m0.033s
  192. user    0m0.033s
  193. sys     0m0.000s
  194.  
  195. real    0m0.035s
  196. user    0m0.034s
  197. sys     0m0.000s
  198.  
  199. # Clang optimised (-O2 -flto -march=native -mfpmath=sse)
  200. # Value
  201. $ for i in {1..10}; do time bin/test; done
  202.  
  203. real    0m0.801s
  204. user    0m0.665s
  205. sys     0m0.136s
  206.  
  207. real    0m0.800s
  208. user    0m0.671s
  209. sys     0m0.128s
  210.  
  211. real    0m0.796s
  212. user    0m0.659s
  213. sys     0m0.136s
  214.  
  215. real    0m0.795s
  216. user    0m0.698s
  217. sys     0m0.096s
  218.  
  219. real    0m0.792s
  220. user    0m0.663s
  221. sys     0m0.129s
  222.  
  223. real    0m0.792s
  224. user    0m0.662s
  225. sys     0m0.128s
  226.  
  227. real    0m0.800s
  228. user    0m0.680s
  229. sys     0m0.120s
  230.  
  231. real    0m0.797s
  232. user    0m0.660s
  233. sys     0m0.133s
  234.  
  235. real    0m0.805s
  236. user    0m0.676s
  237. sys     0m0.128s
  238.  
  239. real    0m0.794s
  240. user    0m0.678s
  241. sys     0m0.116s
  242.  
  243. ## Pointer
  244. $ for i in {1..10}; do time bin/test; done
  245.  
  246. real    0m0.005s
  247. user    0m0.000s
  248. sys     0m0.005s
  249.  
  250. real    0m0.005s
  251. user    0m0.005s
  252. sys     0m0.000s
  253.  
  254. real    0m0.005s
  255. user    0m0.000s
  256. sys     0m0.005s
  257.  
  258. real    0m0.005s
  259. user    0m0.003s
  260. sys     0m0.003s
  261.  
  262. real    0m0.005s
  263. user    0m0.005s
  264. sys     0m0.000s
  265.  
  266. real    0m0.006s
  267. user    0m0.006s
  268. sys     0m0.000s
  269.  
  270. real    0m0.005s
  271. user    0m0.005s
  272. sys     0m0.000s
  273.  
  274. real    0m0.005s
  275. user    0m0.005s
  276. sys     0m0.000s
  277.  
  278. real    0m0.005s
  279. user    0m0.005s
  280. sys     0m0.000s
  281.  
  282. real    0m0.005s
  283. user    0m0.003s
  284. sys     0m0.003s
  285.  
  286. # GCC 4.8 Optimised (same as switches as clang)
  287. ## Value
  288. $ for i in {1..10}; do time bin/test; done
  289. real    0m0.781s
  290. user    0m0.673s
  291. sys     0m0.104s
  292.  
  293. real    0m0.784s
  294. user    0m0.669s
  295. sys     0m0.111s
  296.  
  297. real    0m0.779s
  298. user    0m0.638s
  299. sys     0m0.140s
  300.  
  301. real    0m0.777s
  302. user    0m0.684s
  303. sys     0m0.092s
  304.  
  305. real    0m0.787s
  306. user    0m0.657s
  307. sys     0m0.128s
  308.  
  309. real    0m0.778s
  310. user    0m0.651s
  311. sys     0m0.124s
  312.  
  313. real    0m0.779s
  314. user    0m0.670s
  315. sys     0m0.108s
  316.  
  317. real    0m0.778s
  318. user    0m0.642s
  319. sys     0m0.136s
  320.  
  321. real    0m0.774s
  322. user    0m0.677s
  323. sys     0m0.096s
  324.  
  325. real    0m0.777s
  326. user    0m0.680s
  327. sys     0m0.096s
  328.  
  329. ## Pointer
  330. $ for i in {1..10}; do time bin/test; done
  331.  
  332. real    0m0.010s
  333. user    0m0.010s
  334. sys     0m0.000s
  335.  
  336. real    0m0.010s
  337. user    0m0.010s
  338. sys     0m0.000s
  339.  
  340. real    0m0.011s
  341. user    0m0.011s
  342. sys     0m0.000s
  343.  
  344. real    0m0.009s
  345. user    0m0.006s
  346. sys     0m0.003s
  347.  
  348. real    0m0.010s
  349. user    0m0.009s
  350. sys     0m0.000s
  351.  
  352. real    0m0.010s
  353. user    0m0.010s
  354. sys     0m0.000s
  355.  
  356. real    0m0.010s
  357. user    0m0.010s
  358. sys     0m0.000s
  359.  
  360. real    0m0.010s
  361. user    0m0.009s
  362. sys     0m0.000s
  363.  
  364. real    0m0.010s
  365. user    0m0.009s
  366. sys     0m0.000s
  367.  
  368. real    0m0.011s
  369. user    0m0.010s
  370. sys     0m0.000s
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top