Guest User

Untitled

a guest
Oct 21st, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.07 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "fmt"
  5. "os"
  6. "time"
  7. "strconv"
  8. )
  9.  
  10. func members() []string {
  11. arr := []string{"Haruhi", "Mikuru", "Yuki", "Itsuki", "Kyon"}
  12. return arr
  13. }
  14. func task_strcat1(loop int) string {
  15. m := members()
  16. s1 := m[0]
  17. s2 := m[1]
  18. s3 := m[2]
  19. s4 := m[3]
  20. s5 := m[4]
  21. out := ""
  22. for i := 0; i < loop; i++ {
  23. buf := ""
  24. buf = "<table>\n"
  25. for j := 0; j < 20; j++ {
  26. 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"
  27. }
  28. buf += "</table>\n"
  29. out = buf
  30. }
  31. //verify(out)
  32. return out
  33. }
  34.  
  35. func task_strcat_orig(loop int) string {
  36. m := members()
  37. s1 := m[0]; s2 := m[1]; s3 := m[2]; s4 := m[3]; s5 := m[4];
  38. out := ""
  39. for i := 0; i < loop; i++ {
  40. buf := ""
  41. buf += `<table>
  42. `; for j := 0; j < 20; j++ {
  43. buf += ` <tr>
  44. <td>` + s1 + `</td>
  45. <td>` + s2 + `</td>
  46. <td>` + s3 + `</td>
  47. <td>` + s4 + `</td>
  48. <td>` + s5 + `</td>
  49. </tr>
  50. `; }
  51. buf += `</table>
  52. `;
  53. out = buf
  54. }
  55. return out
  56. }
  57. func run(loop int, empty_time float64, body func(int) string, title string) float64 {
  58. fmt.Printf("%-45s", title)
  59. start := time.Nanoseconds()
  60. body(loop)
  61. stop := time.Nanoseconds()
  62. real := float64(stop-start) / (1000 * 1000 * 1000)
  63. actual := real - empty_time
  64. fmt.Printf(" %10.3f %10.3f\n", real, actual)
  65. return real
  66. }
  67.  
  68. func main() {
  69. loop := 100000
  70. if os.Getenv("N") != "" {
  71. n, err := strconv.Atoi(os.Getenv("N"))
  72. if err != nil {
  73. os.Stderr.WriteString("*** can't convert N\n")
  74. } else {
  75. loop = n
  76. }
  77. }
  78. fmt.Printf("*** loop=%d\n", loop)
  79. cycle := 3
  80. if os.Getenv("C") != "" {
  81. c, err := strconv.Atoi(os.Getenv("C"))
  82. if err != nil {
  83. os.Stderr.WriteString("*** can't convert C\n")
  84. } else {
  85. cycle = c
  86. }
  87. }
  88. for i := 1; i <= cycle; i++ {
  89. fmt.Printf("\n")
  90. fmt.Printf("%-45s %10s %10s\n", "# cycle="+strconv.Itoa(i), "real", "actual")
  91. empty_time := 0.0
  92. run(loop, empty_time, task_strcat1, `str+=s1+s2+s3`)
  93. run(loop, empty_time, task_strcat_orig, `(original) str+=s1+s2+s3`)
  94. }
  95. }
Add Comment
Please, Sign In to add comment