Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from json import dumps as json_encode
- from random import randint # micropython random does not have suffle
- import time
- # Note this is written for micropython, this is a test to see how different methods of merging strings preform
- # * it seems + is better for 2 small strings, but with more complex stuff it starts loosing ground to other methods
- # * based on results using a RP2040 PICO
- def shuffle(arr):
- n = len(arr)
- for i in range(n):
- j = randint(0, n-1)
- element = arr.pop(j)
- arr.append(element)
- json={# https://json.org/example.html
- "glossary": {
- "title": "example glossary",
- "GlossDiv": {
- "title": "S",
- "GlossList": {
- "GlossEntry": {
- "ID": "SGML",
- "SortAs": "SGML",
- "GlossTerm": "Standard Generalized Markup Language",
- "Acronym": "SGML",
- "Abbrev": "ISO 8879:1986",
- "GlossDef": {
- "para": "A meta-markup language, used to create markup languages such as DocBook.",
- "GlossSeeAlso": [
- "GML",
- "XML"
- ]
- },
- "GlossSee": "markup"
- }
- }
- }
- }
- }
- json=json_encode(json)# yea PHP naming deal with it
- def join():
- print(".join")
- avg=0
- for i in range(10):
- then=time.ticks_cpu()
- ''.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])
- now=time.ticks_cpu()
- ticks=time.ticks_diff(now,then)
- print(ticks,"- Sample:",i+1)
- avg+=ticks
- print(avg/10,"- Average")
- def plus():
- print("+ Operator")
- avg=0
- for i in range(10):
- then=time.ticks_cpu()
- '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
- now=time.ticks_cpu()
- ticks=time.ticks_diff(now,then)
- print(ticks,"- Sample:",i+1)
- avg+=ticks
- print(avg/10,"- Average")
- def dot_format():
- print(".format")
- avg=0
- for i in range(10):
- then=time.ticks_cpu()
- '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)
- now=time.ticks_cpu()
- ticks=time.ticks_diff(now,then)
- print(ticks,"- Sample:",i+1)
- avg+=ticks
- print(avg/10,"- Average")
- def fstring():
- print("fstring")
- avg=0
- for i in range(10):
- then=time.ticks_cpu()
- 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}'
- now=time.ticks_cpu()
- ticks=time.ticks_diff(now,then)
- print(ticks,"- Sample:",i+1)
- avg+=ticks
- print(avg/10,"- Average")
- def pct():
- print("% Operator")
- avg=0
- for i in range(10):
- then=time.ticks_cpu()
- '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)
- now=time.ticks_cpu()
- ticks=time.ticks_diff(now,then)
- print(ticks,"- Sample:",i+1)
- avg+=ticks
- print(avg/10,"- Average")
- lst=[join, plus, dot_format, fstring, pct]
- shuffle(lst)
- for i in range(0,len(lst)):
- if i:
- print()
- lst[i]()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement