test-str-ushort.nut 1003 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. local str = "\x10\x10";
  2. print(str[0], str[1], str.uchar(0), str.uchar(1), str.ushort(0), (str.uchar(0) * 256) + str.uchar(1));
  3. auto max_loop = 64 * 1024;
  4. str = str.rep(max_loop);
  5. auto start_time = os.clock();
  6. for(auto i=0; i < max_loop; ++i)
  7. {
  8. auto ch = str[i];
  9. }
  10. print("Time spent", max_loop, os.clock() - start_time);
  11. start_time = os.clock();
  12. for(auto i=0; i < max_loop; ++i)
  13. {
  14. auto ch = str.uchar(i);
  15. }
  16. print("Time spent", max_loop, os.clock() - start_time);
  17. start_time = os.clock();
  18. for(auto i=0, len = max_loop/2; i < len; ++i)
  19. {
  20. auto ch = str.ushort(i);
  21. }
  22. print("Time spent", max_loop, os.clock() - start_time);
  23. start_time = os.clock();
  24. for(auto i=0, len = max_loop/2; i < len; ++i)
  25. {
  26. auto ch = (str[i] * 256) + str[i+1];
  27. }
  28. print("Time spent", max_loop, os.clock() - start_time);
  29. local buf = blob();
  30. buf.write(str);
  31. buf.seek(0);
  32. start_time = os.clock();
  33. for(auto i=0, len = max_loop/2; i < len; ++i)
  34. {
  35. auto ch = buf.readn('w');
  36. }
  37. print("Time spent", max_loop, os.clock() - start_time);