ch1/ch1-02 #140
Replies: 23 comments 4 replies
-
练习1.1 和 练习 1.2 for idx, arg := range os.Args {
if idx == 0 {
fmt.Println("os.Args[0]=", arg)
}
fmt.Println(idx, "=", arg)
} |
Beta Was this translation helpful? Give feedback.
-
package main import ( func main() {
} 练习3 测试第一个代码块的运行时间是512.5µs 第二个代码块的时间是 507.6µs 可以通过注释来分别运行 或者打印fmt.PrintIn(end)和now的时间来看具体的时间 |
Beta Was this translation helpful? Give feedback.
-
练习 1.2 for i, v := range os.Args {
fmt.Printf("%d\t%s\n", i, v)
} |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
练习1.2 import ( func main() {
} |
Beta Was this translation helpful? Give feedback.
-
echo1、2、3没有输出啊,我的输入是啥呢 |
Beta Was this translation helpful? Give feedback.
-
package main
import (
"fmt"
"os"
"strings"
"time"
)
func main() {
s, sep := "", "\r\n"
now := time.Now()
for _, arg := range os.Args[:] {
s += arg + sep
}
fmt.Println(s)
end := time.Now()
fmt.Println("运行时间", end.Sub(now))
now2 := time.Now()
fmt.Println(strings.Join(os.Args[:], "\r\n"))
end2 := time.Now()
fmt.Println("运行时间", end2.Sub(now2))
}
// console
/go-build3281070192/b001/exe/hello
a=123
b=456
运行时间 18.984µs
/go-build3281070192/b001/exe/hello
a=123
b=456
运行时间 1.403µs 差了 18 倍??? |
Beta Was this translation helpful? Give feedback.
-
//1.3 |
Beta Was this translation helpful? Give feedback.
-
练习 1.3 第一种40s,第二种不稳定比40s快一点,第三种在28s左右 测试数据
|
Beta Was this translation helpful? Give feedback.
-
练习1.1 fmt.Println(os.Args[0]) 练习1.2 import (
"fmt"
"os"
"strconv"
)
func main(){
var s, sep string
for idx, args := range os.Args[1:] {
sep = " "
s += strconv.Itoa(idx) + sep _+ args
}
fmt.Println(s)
} 练习1.3 import (
"fmt"
"os"
"time"
"strings"
)
func low_func(){
var s, sep string
start := time.Now()
for _, args := range os.Args[1:] {
s += sep + args
sep = " "
}
fmt.Println(s)
end := time.Now()
fmt.Println("low running time:", end.Sub(start))
}
func high_func(){
start := time.Now()
fmt.Println(strings.Join(os.Args[1:], " "))
end := time.Now()
fmt.Println("high running time:", end.Sub(start))
}
func main(){
low_func()
high_func()
} |
Beta Was this translation helpful? Give feedback.
-
感觉差距不大,测试字符串太少了 74 都波动在504-530微秒以内func main() {
forOutput()
fmt.Println("===========================")
stringsJoinOut()
}
func forOutput() {
start := time.Now()
fmt.Println("1.使用for循环方式打印")
fmt.Println(os.Args[0])
var s, sep string
for i := 1; i < len(os.Args); i++ {
s += sep + os.Args[i]
sep = " "
}
fmt.Println(s, len(os.Args))
fmt.Println("运行时间:", time.Since(start).Microseconds())
}
func stringsJoinOut() {
start := time.Now()
fmt.Println("2.使用strings.Join打印")
fmt.Println(os.Args[0])
fmt.Println(strings.Join(os.Args[1:], " "), len(os.Args))
fmt.Println("运行时间:", time.Since(start).Microseconds())
} |
Beta Was this translation helpful? Give feedback.
-
strings.Join快的原因是先计算了所有待拼接字符串的总长度,然后一次性分配内存. |
Beta Was this translation helpful? Give feedback.
-
strings.Join()是一次性分配的地址,所以快很多 |
Beta Was this translation helpful? Give feedback.
-
for i, arg := range os.Args[1:] {
println("index: ", i, "value: ", arg)
} |
Beta Was this translation helpful? Give feedback.
-
练习1.2
|
Beta Was this translation helpful? Give feedback.
-
1.1、1.2
} |
Beta Was this translation helpful? Give feedback.
-
练习1 func test1() {
fmt.Println(strings.Join(os.Args, " "))
} 练习2 func test2() {
for index, item := range os.Args[1:] {
fmt.Println(index, item)
}
} 练习3 func test3() {
//先构造一个长字符串
s := os.Args[1:]
for i := 0; i < 100; i++ {
s = append(s, s[0])
s = append(s, s[1])
s = append(s, s[2])
}
start1 := time.Now()
var result1, step1 string = "", ""
for _, item := range s {
result1 += step1 + item
step1 = ""
}
fmt.Println(result1)
fmt.Printf("time.Now().Sub(start1): %v\n", time.Now().Sub(start1))
start2 := time.Now()
result2 := strings.Join(s, " ")
fmt.Println(result2)
fmt.Printf("time.Now().Sub(start3): %v\n", time.Now().Sub(start2))
} |
Beta Was this translation helpful? Give feedback.
-
练习1.1 package main import ( func main() { |
Beta Was this translation helpful? Give feedback.
-
// 1.1 func main() {
fmt.Println(strings.Join(os.Args[0:], " "))
} // 1.2 func main() {
for idx, arg := range os.Args {
fmt.Println(idx, "=", arg)
}
} |
Beta Was this translation helpful? Give feedback.
-
练习··· package main
import (
"fmt"
"os"
"time"
"strings"
)
func main() {
var s, sep string
begin1 := time.Now()
for index, arg := range os.Args[0:] {
fmt.Printf("Index: %d, arg: %s\n", index, arg)
}
end1 := time.Now()
fmt.Println("time1:", end1.Sub(begin1))
begin2 := time.Now()
for _, arg := range os.Args[1:] {
s += sep + arg
sep = " "
}
fmt.Println(s)
end2 := time.Now()
fmt.Println("time2:", end2.Sub(begin2))
begin3 := time.Now()
fmt.Println(strings.Join(os.Args[1:], " "))
end3 := time.Now()
fmt.Println("time3:", end3.Sub(begin3))
} 用了之前一位仁兄的测试输入,不知道为什么总有0s的 Index: 0, arg: C:\Users\yukin\AppData\Local\Temp\go-build57938929\b001\exe\package main.exe
Index: 1, arg: op
Index: 2, arg: 81
Index: 3, arg: 213
Index: 4, arg: 19
Index: 5, arg: 0
Index: 6, arg: 12
Index: 7, arg: 90
Index: 8, arg: 66
Index: 9, arg: sidhaifgaifiaf
Index: 10, arg: edadhiuefiauhdiaheuarueabrdabfbaeuiwiajoejqioejoj12iejore
Index: 11, arg: 13143ih
Index: 12, arg: 77da
Index: 13, arg: 12131rvf
time1: 0s
op 81 213 19 0 12 90 66 sidhaifgaifiaf edadhiuefiauhdiaheuarueabrdabfbaeuiwiajoejqioejoj12iejore 13143ih 77da 12131rvf
time2: 523.6µs
op 81 213 19 0 12 90 66 sidhaifgaifiaf edadhiuefiauhdiaheuarueabrdabfbaeuiwiajoejqioejoj12iejore 13143ih 77da 12131rvf
time3: 51.4µs 第二次编译 Index: 0, arg: C:\Users\yukin\AppData\Local\Temp\go-build1108872667\b001\exe\package main.exe
Index: 1, arg: op
Index: 2, arg: 81
Index: 3, arg: 213
Index: 4, arg: 19
Index: 5, arg: 0
Index: 6, arg: 12
Index: 7, arg: 90
Index: 8, arg: 66
Index: 9, arg: sidhaifgaifiaf
Index: 10, arg: edadhiuefiauhdiaheuarueabrdabfbaeuiwiajoejqioejoj12iejore
Index: 11, arg: 13143ih
Index: 12, arg: 77da
Index: 13, arg: 12131rvf
time1: 3.3041ms
op 81 213 19 0 12 90 66 sidhaifgaifiaf edadhiuefiauhdiaheuarueabrdabfbaeuiwiajoejqioejoj12iejore 13143ih 77da 12131rvf
time2: 0s
op 81 213 19 0 12 90 66 sidhaifgaifiaf edadhiuefiauhdiaheuarueabrdabfbaeuiwiajoejqioejoj12iejore 13143ih 77da 12131rvf
time3: 0s 第三次 Index: 0, arg: C:\Users\yukin\AppData\Local\Temp\go-build2495421148\b001\exe\package main.exe
Index: 1, arg: op
Index: 2, arg: 81
Index: 3, arg: 213
Index: 4, arg: 19
Index: 5, arg: 0
Index: 6, arg: 12
Index: 7, arg: 90
Index: 8, arg: 66
Index: 9, arg: sidhaifgaifiaf
Index: 10, arg: edadhiuefiauhdiaheuarueabrdabfbaeuiwiajoejqioejoj12iejore
Index: 11, arg: 13143ih
Index: 12, arg: 77da
Index: 13, arg: 12131rvf
time1: 1.5424ms
op 81 213 19 0 12 90 66 sidhaifgaifiaf edadhiuefiauhdiaheuarueabrdabfbaeuiwiajoejqioejoj12iejore 13143ih 77da 12131rvf
time2: 526.6µs
op 81 213 19 0 12 90 66 sidhaifgaifiaf edadhiuefiauhdiaheuarueabrdabfbaeuiwiajoejqioejoj12iejore 13143ih 77da 12131rvf
time3: 0s |
Beta Was this translation helpful? Give feedback.
-
ch1/ch1-02
中文版
https://gopl-zh.github.io/ch1/ch1-02.html
Beta Was this translation helpful? Give feedback.
All reactions