Advertisement
Guest User

concat.py

a guest
Sep 25th, 2024
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.89 KB | Software | 0 0
  1. from json import dumps as json_encode
  2. from random import randint # micropython random does not have suffle
  3. import time
  4.  
  5. # Note this is written for micropython, this is a test to see how different methods of merging strings preform
  6. #  * it seems + is better for 2 small strings, but with more complex stuff it starts loosing ground to other methods
  7. #  * based on results using a RP2040 PICO
  8.  
  9. def shuffle(arr):
  10.         n = len(arr)
  11.         for i in range(n):
  12.                 j = randint(0, n-1)
  13.                 element = arr.pop(j)
  14.                 arr.append(element)
  15.  
  16. json={# https://json.org/example.html
  17.     "glossary": {
  18.         "title": "example glossary",
  19.         "GlossDiv": {
  20.             "title": "S",
  21.             "GlossList": {
  22.                 "GlossEntry": {
  23.                     "ID": "SGML",
  24.                     "SortAs": "SGML",
  25.                     "GlossTerm": "Standard Generalized Markup Language",
  26.                     "Acronym": "SGML",
  27.                     "Abbrev": "ISO 8879:1986",
  28.                     "GlossDef": {
  29.                         "para": "A meta-markup language, used to create markup languages such as DocBook.",
  30.                         "GlossSeeAlso": [
  31.                             "GML",
  32.                             "XML"
  33.                         ]
  34.                     },
  35.                     "GlossSee": "markup"
  36.                 }
  37.             }
  38.         }
  39.     }
  40. }
  41. json=json_encode(json)# yea PHP naming deal with it
  42.  
  43. def join():
  44.         print(".join")
  45.         avg=0
  46.         for i in range(10):
  47.                 then=time.ticks_cpu()
  48.                 ''.join(['HTTP/1.0 200 OK\r\nContent-type: application/json\r\nContent-Length: ',str(len(json)),'\r\nCache-Control: no-cache\r\n\r\n',json])
  49.                 now=time.ticks_cpu()
  50.                 ticks=time.ticks_diff(now,then)
  51.                 print(ticks,"- Sample:",i+1)
  52.                 avg+=ticks
  53.         print(avg/10,"- Average")
  54.  
  55. def plus():
  56.         print("+ Operator")
  57.         avg=0
  58.         for i in range(10):
  59.                 then=time.ticks_cpu()
  60.                 'HTTP/1.0 200 OK\r\nContent-type: application/json\r\nContent-Length: '+str(len(json))+'\r\nCache-Control: no-cache\r\n\r\n'+json
  61.                 now=time.ticks_cpu()
  62.                 ticks=time.ticks_diff(now,then)
  63.                 print(ticks,"- Sample:",i+1)
  64.                 avg+=ticks
  65.         print(avg/10,"- Average")
  66.  
  67. def dot_format():
  68.         print(".format")
  69.         avg=0
  70.         for i in range(10):
  71.                 then=time.ticks_cpu()
  72.                 'HTTP/1.0 200 OK\r\nContent-type: application/json\r\nContent-Length: {}\r\nCache-Control: no-cache\r\n\r\n{}'.format(len(json),json)
  73.                 now=time.ticks_cpu()
  74.                 ticks=time.ticks_diff(now,then)
  75.                 print(ticks,"- Sample:",i+1)
  76.                 avg+=ticks
  77.         print(avg/10,"- Average")
  78.  
  79. def fstring():
  80.         print("fstring")
  81.         avg=0
  82.         for i in range(10):
  83.                 then=time.ticks_cpu()
  84.                 f'HTTP/1.0 200 OK\r\nContent-type: application/json\r\nContent-Length: {len(json)}\r\nCache-Control: no-cache\r\n\r\n{json}'
  85.                 now=time.ticks_cpu()
  86.                 ticks=time.ticks_diff(now,then)
  87.                 print(ticks,"- Sample:",i+1)
  88.                 avg+=ticks
  89.         print(avg/10,"- Average")
  90.  
  91. def pct():
  92.         print("% Operator")
  93.         avg=0
  94.         for i in range(10):
  95.                 then=time.ticks_cpu()
  96.                 'HTTP/1.0 200 OK\r\nContent-type: application/json\r\nContent-Length: %d\r\nCache-Control: no-cache\r\n\r\n%s' % (len(json), json)
  97.                 now=time.ticks_cpu()
  98.                 ticks=time.ticks_diff(now,then)
  99.                 print(ticks,"- Sample:",i+1)
  100.                 avg+=ticks
  101.         print(avg/10,"- Average")
  102.  
  103. lst=[join, plus, dot_format, fstring, pct]
  104. shuffle(lst)
  105. for i in range(0,len(lst)):
  106.         if i:
  107.                 print()
  108.         lst[i]()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement