moebius1977

Profiling: array of UDTs vs UDT of arrays

Using Stopwatch Library by PineCoders, I am trying to test which is faster, an array of user-defined type (UDT) objects vs an object with many child arrays.

The task is to store and manipulate array of objects having total 9 values: 4 floats, 4 strings and 1 int.

Option 1: create a UDT with 9 fields and store an array of such UDT objects.
Option 2: create a UDT with 9 arrays individually for each value.

The test task is of three stages:

  • Populate array(s) with some (timenow) values - in the options you can choose how many values to push into the array/arrays. Note that max size of array(s) is set independently, so you can push 1000 of elements into an array capped at 100 max size and as new elements will be pushed (added to the end) the old exceeding elements will be shifted (removed from the beginning)
  • Write - write to random elements of the array. Two options for writing to a UDT object: (1) assign to each field independently, (2) create a UDT object and use array.set() function.
  • Read - read from random elements of the array.
    In the options you can how many times per bar to run each of the steps (same number for each step).

I tested by adding three indicators to the chart and choosing different options for each:
1. Array of UDT's where writing is done by creating a new UDT from the values and then using set(udt)
2. Array of UDT's where writing is done by assigning the value of each of the properties of the UDT individually (saving time on creating of a new object).
3. UDT of arrays.

As of 16 Arpil 2023 the UDT of arrays seems about 20-30% faster than the array of UDT's with setting each property without creating new UDT object.
สคริปต์โอเพนซอร์ซ

ด้วยจิตวิญญาณของ TradingView อย่างแท้จริง ผู้เขียนสคริปต์นี้ได้เผยแพร่เป็นโอเพนซอร์ส เพื่อให้ผู้ค้าสามารถเข้าใจและตรวจสอบได้ ไชโยให้กับผู้เขียน! คุณสามารถใช้ได้ฟรี แต่การใช้รหัสนี้ซ้ำในสิ่งพิมพ์อยู่ภายใต้กฎระเบียบการใช้งาน คุณสามารถตั้งเป็นรายการโปรดเพื่อใช้บนชาร์ตได้

คำจำกัดสิทธิ์ความรับผิดชอบ

ข้อมูลและบทความไม่ได้มีวัตถุประสงค์เพื่อก่อให้เกิดกิจกรรมทางการเงิน, การลงทุน, การซื้อขาย, ข้อเสนอแนะ หรือคำแนะนำประเภทอื่น ๆ ที่ให้หรือรับรองโดย TradingView อ่านเพิ่มเติมที่ เงื่อนไขการใช้บริการ

ต้องการที่จะใช้สคริปต์นี้บนชาร์ตใช่ไหม?