mirror of
https://github.com/krahets/hello-algo.git
synced 2026-07-13 15:56:05 +00:00
build
This commit is contained in:
@@ -7,7 +7,7 @@ comments: true
|
||||
运行时间可以直观且准确地反映算法的效率。如果我们想要准确预估一段代码的运行时间,应该如何操作呢?
|
||||
|
||||
1. **确定运行平台**,包括硬件配置、编程语言、系统环境等,这些因素都会影响代码的运行效率。
|
||||
2. **评估各种计算操作所需的运行时间**,例如加法操作 `+` 需要 1 ns,乘法操作 `*` 需要 10 ns,打印操作 `print()` 需要 5 ns 等。
|
||||
2. **评估各种计算操作所需的运行时间**,例如加法操作 `+` 需要 1 ns ,乘法操作 `*` 需要 10 ns ,打印操作 `print()` 需要 5 ns 等。
|
||||
3. **统计代码中所有的计算操作**,并将所有操作的执行时间求和,从而得到运行时间。
|
||||
|
||||
例如在以下代码中,输入数据大小为 $n$ :
|
||||
@@ -3260,7 +3260,7 @@ $$
|
||||
```c title="worst_best_time_complexity.c"
|
||||
/* 生成一个数组,元素为 { 1, 2, ..., n },顺序被打乱 */
|
||||
int *randomNumbers(int n) {
|
||||
// 分配堆区内存(创建一维可变长数组:数组中元素数量为n,元素类型为int)
|
||||
// 分配堆区内存(创建一维可变长数组:数组中元素数量为 n ,元素类型为 int )
|
||||
int *nums = (int *)malloc(n * sizeof(int));
|
||||
// 生成数组 nums = { 1, 2, 3, ..., n }
|
||||
for (int i = 0; i < n; i++) {
|
||||
|
||||
Reference in New Issue
Block a user