解釋上述程式碼運作原理
撰寫時間量測程式碼
timespec
,實作計算時間消耗的函示 diff_in_second
:struct timespec start, end;
double cpu_time1, cpu_time2;
static double diff_in_second(struct timespec t1, struct timespec t2)
{
struct timespec diff;
if (t2.tv_nsec-t1.tv_nsec < 0) {
diff.tv_sec = t2.tv_sec - t1.tv_sec - 1;
diff.tv_nsec = t2.tv_nsec - t1.tv_nsec + 1000000000;
} else {
diff.tv_sec = t2.tv_sec - t1.tv_sec;
diff.tv_nsec = t2.tv_nsec - t1.tv_nsec;
}
return (diff.tv_sec + diff.tv_nsec / 1000000000.0);
}
clock_gettime(CLOCK_REALTIME, &start);
for (long int j = 0; j < num; ++j) {
treeint_insert(rand() % (num - 1));
}
clock_gettime(CLOCK_REALTIME, &end);
t1 = diff_in_second(start, end);
其中 num
為需要 insert/remove 的節點總數。對應 remove 的部分:
clock_gettime(CLOCK_REALTIME, &start);
for (long int k = 0; k < num; ++k) {
int v = rand() % (num - 1);
treeint_remove(v);
}
clock_gettime(CLOCK_REALTIME, &end);
t2 = diff_in_second(start, end);
import numpy as np
import matplotlib .pyplot as plt
if __name__ == "__main__":
output = np.loadtxt('data.txt', dtype='float').T
X = output[0]
fig, ax = plt.subplots(1, 1, sharey=True)
ax.set_title('Time consumption', fontsize=16)
ax.set_xlabel('numbers of insert/remove', fontsize=16)
ax.set_ylabel('time (s)', fontsize=16)
ax.plot(X, output[1], marker='+', markersize=3, label='insert')
ax.plot(X, output[2], marker='*', markersize=3, label='remove')
ax.legend(loc='upper left')
plt.show()
MacBook Pro (16-inch, 2019)
處理器:2.3 GHz 8 核心 Intel Core i9
Learn More →
觀察數值為 2 的冪的倍數時,其二進為表示法的最低
align_up(120, 4)
輸出結果為 120
align_up(121, 4)
輸出結果為 124
align_up(122, 4)
輸出結果為 124
align_up(124, 4)
輸出結果為 124
align_up(120, 4)
的結果。align_up(121, 4)
後為二進位 1111100,既然是向上對齊,考慮加上對齊值 (121 + 4) &~ (4-1) = 124
(122 + 4) &~ (4-1) = 124
(123 + 4) &~ (4-1) = 124
(124 + 4) &~ (4-1) = 128
,然而 124 做 align_up(124, 4) 是 124。觀察 124 + 4 的二進位,其第 2 位元發生進位,為避免此情況,改寫為 (124 + (4-1)) &~ (4-1) = 124
完整程式碼:
#include <stdint.h>
static inline uintptr_t align_up(uintptr_t sz, size_t alignment)
{
uintptr_t mask = alignment - 1;
if ((alignment & mask) == 0) { /* power of two? */
return (((sz) + (mask)) & ~(mask));
}
return (((sz + mask) / alignment) * alignment);
}
營收 (Revenue) 是一間公司「做多少生意」的指標,然而做多少生意不表示賺多少錢,營收再高也不代表賺更多的錢,中間還要扣除銷貨成本得到的營業毛利 (Gross profit),營業毛利扣掉費用後得到的營業利益 (Operating profit),而後還有營業外支出,利息與所得稅等等,東扣西扣最後才是代表轉多少錢的指標稅後淨利 (Net income)。
Dec 15, 2021問題 什麼因素讓你支持(購買)加密貨幣? 根據方舟基金 (Ark Investiment) 的研究報告,加密貨幣佔個人總資產的 5% 可達到收益最大化。若加以研究,輔以量化分析方式,預期帶來比定期定額投入更大的收益。 加上加密貨幣是新興市場,市場效率較傳統的金融市場落後,光是用技術指標就可以做出擊敗大盤的策略。 展開談談 「數據基本面」: 籌碼面 個人對於加密貨幣的籌碼面較有興趣,例如多空比,合約持倉量以及資金費率。基於大戶與機構投資人看好商品的未來而投入的籌碼,所以相信籌碼面是基本面的延伸。 籌碼的分佈亦反映市場的情緒,以加密貨幣市場為例,遠程合約或永續合約與現貨(指數)的價差可以反映市場的熱度,如果合約價格與現貨價格呈現正價差,則市場普遍看多。反之如果合約價格與現貨價格呈現負價差,則市場普遍看空。
Dec 10, 2021or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up