Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "fmt"
- "os"
- "time"
- "strconv"
- )
- func members() []string {
- arr := []string{"Haruhi", "Mikuru", "Yuki", "Itsuki", "Kyon"}
- return arr
- }
- func task_strcat1(loop int) string {
- m := members()
- s1 := m[0]
- s2 := m[1]
- s3 := m[2]
- s4 := m[3]
- s5 := m[4]
- out := ""
- for i := 0; i < loop; i++ {
- buf := ""
- buf = "<table>\n"
- for j := 0; j < 20; j++ {
- buf += " <tr>\n<td>" + s1 + "</td>\n<td>" + s2 + "</td>\n<td>" + s3 + "</td>\n<td>" + s4 + "</td>\n<td>" + s5 + "</td>\n</tr>\n"
- }
- buf += "</table>\n"
- out = buf
- }
- //verify(out)
- return out
- }
- func task_strcat_orig(loop int) string {
- m := members()
- s1 := m[0]; s2 := m[1]; s3 := m[2]; s4 := m[3]; s5 := m[4];
- out := ""
- for i := 0; i < loop; i++ {
- buf := ""
- buf += `<table>
- `; for j := 0; j < 20; j++ {
- buf += ` <tr>
- <td>` + s1 + `</td>
- <td>` + s2 + `</td>
- <td>` + s3 + `</td>
- <td>` + s4 + `</td>
- <td>` + s5 + `</td>
- </tr>
- `; }
- buf += `</table>
- `;
- out = buf
- }
- return out
- }
- func run(loop int, empty_time float64, body func(int) string, title string) float64 {
- fmt.Printf("%-45s", title)
- start := time.Nanoseconds()
- body(loop)
- stop := time.Nanoseconds()
- real := float64(stop-start) / (1000 * 1000 * 1000)
- actual := real - empty_time
- fmt.Printf(" %10.3f %10.3f\n", real, actual)
- return real
- }
- func main() {
- loop := 100000
- if os.Getenv("N") != "" {
- n, err := strconv.Atoi(os.Getenv("N"))
- if err != nil {
- os.Stderr.WriteString("*** can't convert N\n")
- } else {
- loop = n
- }
- }
- fmt.Printf("*** loop=%d\n", loop)
- cycle := 3
- if os.Getenv("C") != "" {
- c, err := strconv.Atoi(os.Getenv("C"))
- if err != nil {
- os.Stderr.WriteString("*** can't convert C\n")
- } else {
- cycle = c
- }
- }
- for i := 1; i <= cycle; i++ {
- fmt.Printf("\n")
- fmt.Printf("%-45s %10s %10s\n", "# cycle="+strconv.Itoa(i), "real", "actual")
- empty_time := 0.0
- run(loop, empty_time, task_strcat1, `str+=s1+s2+s3`)
- run(loop, empty_time, task_strcat_orig, `(original) str+=s1+s2+s3`)
- }
- }
Add Comment
Please, Sign In to add comment