Pineプロファイラについて詳しく知りたいのですが


What is the Pine Profiler, and how do I use it?

The Pine Profiler is a powerful utility that analyzes the executions of all significant code lines and blocks in a Pine script and displays helpful performance information next to each line inside the Pine Editor. By inspecting the Profiler's results, programmers can gain a clearer perspective on a script's overall runtime, the distribution of runtime across its significant code regions, and the critical portions that may need extra attention and optimization.

To profile a script with Pine Profiler, follow these steps:

  1. Open the source code of the script in your Pine Editor. If this script is read-only, make a copy of it.
  2. Add the script to the chart.
  3. Click on the active "Profiler mode" button in the dropdown next to the "Publish indicator" option in the top-right corner:

Once enabled, the Profiler collects information from all executions of the script's significant code regions and displays approximate runtime percentages to the left of the code lines inside the Pine Editor.

Hovering over the left margin next to the lines in the Pine Editor will highlight an analyzed code region and reveal a small window with additional performance information, including the line numbers of the analyzed code, the time spent on that code compared to the script's total runtime, and the code's total number of executions:

The fields the window will show depend on the analyzed code. If the profiled code is a single line, it will display three fields: "Line number", "Time", and "Executions". The results shown for the line at the start of a loop or conditional structure will show a "Code block range" field instead of "Line number", and it will include an additional "Line time" field.

Note: The Pine Profiler wraps each significant line and code block with the extra calculations required to track and display performance results. Consequently, the sum of the percentages shown for all lines in the script will not sum up to 100%, and the sum of all the profiled regions' time results will be less than the script's total runtime. 

When a script contains at least four significant lines of code, the Profiler will include "flame" icons to the left of the top three code regions with the highest impact on runtime performance. If a high-impact code is outside the lines visible inside the Pine Editor, a "flame" icon and a number will appear at the top or bottom of the left margin. Clicking that icon will vertically scroll the Editor's view to show the nearest high-impact line:

To learn more about the Profiler's features, how to interpret its results, and some tips on optimizing Pine Script™ code, see the Profiling and optimization page in our Pine Script™ User Manual.

- jp


Pineプロファイラとは何ですか、どのように使うのですか?

Pineプロファイラは強力なユーティリティで、スクリプト内の重要なコード行とブロックすべてについて実行時の状態を解析し、 Pineエディタ各行の隣にそのパフォーマンスに関して役立つ情報を表示するものです。プロファイラの解析結果を調べると、スクリプト全体の実行時間やそれに対して重要なコード領域の実行時間が占める割合がわかり、特に注意を払うべき部分や最適化が必要と思われる箇所を明確に見つけることができます。

Pineプロファイラでスクリプトをプロファイルする手順は下記のとおりです:

  1. Pineエディタでスクリプトのソースコードを開きます。スクリプトが読み込み専用の場合は、コピーを作成してください。
  2. チャートにスクリプトを追加します。
  3. エディタ画面右上の 「インジケーターを投稿」オプションの横にあるドロップダウンメニュー内の「プロファイラーモード」のスイッチをオンにしてください:

プロファイラを有効にすると、スクリプト内の重要なコード領域すべてから実行時の情報が収集され、全体の実行時間に対して占める割合の概算値がPineエディタのコード行の左側に表示されます。

Pineエディタで行の左側の余白にマウスカーソルを置くと解析されたコード領域が強調表示され、コードの行番号、スクリプト全体の実行時間とそのコードに費やされた時間、コードの総実行回数といったパフォーマンスに関する詳細情報が小さなウィンドウに表示されます:

ウィンドウに表示されるフィールドは解析されたコードによって異なります。プロファイルされたコードが1行の場合、「行番号 (Line number)」、「時間 (Time)」、「実行回数 (Executions)」の3つのフィールドが表示されます。ループや条件構造の開始行では「行番号」のかわりに「コードブロック範囲 (Code block range)」フィールドが表示され、さらに「行時間 (Line time)」フィールドが追加で表示されます。

注意: Pineプロファイラは、パフォーマンスの結果を追跡しながら表示を行うため、それに必要な追加の計算を重要な行およびコードブロックごとに行います。その結果、スクリプト内のすべての行に表示されるパーセント値の合計は100%にならず、プロファイル領域で算出された時間をすべて合計してもスクリプト全体の実行時間より短くなります。

スクリプトに重要なコードが4行以上含まれる場合、プロファイラは実行時間のパフォーマンスに与える影響が大きい上位3つのコード領域の左側に「炎」のアイコンを表示します。影響度の高いコードがPineエディタ画面の範囲外にある場合は、画面左の余白部分の上部または下部に「炎」のアイコンと数字が表示されます。このアイコンをクリックするとエディター画面が縦にスクロールして、影響度の高い行の中で一番近くにあるものが表示されます:

プロファイラの機能や結果の見方、Pine Script™ コードの最適化のヒントについてさらに詳細を知りたい場合は、Pine Script™ ユーザーマニュアルの Profiling and optimization のページをご覧ください。