See %SQL.StatementResult:%DisplayFormatted()

Simple sample (for namespace "SAMPLES"):

set st ##class(%SQL.Statement).%New(2,"Sample")
set sql = 3
set sql(1) = "select TOP 5 %ID as id, Name, DOB, Home_State"
set sql(2) = "from Person where Age > 40"
set sql(3) = "order by 2"
do st.%Prepare(.sql)
for type="txt","pdf","csv","html","xml" {
  set rs st.%Execute()
  do rs.%DisplayFormatted(type,"C:\Temp\report")
}

As a result, the following files are generated:

report.csv
report.html
report.pdf
report.txt
report.xml

Instead of

set str=$e(str,1,*-1)

, will be a little faster

set $e(str,*)=""

But this is already saving on matches.

PS: for the sake of interest, I checked on a special version of Caché (very old) with server-side JavaScript (not to be confused with Node.js)

 
Source code
USER>##class(dc.test).Test(1800000)
[JavaScript] execution: 99(ms) len(str): 3599999
[COS] execution: 126.683(ms) len(str): 3599999

By the way, the JS code has almost no limit on the string size.

 
An example without complex formulas and very fast

Simple sample:

Class dc.test Extends %ZEN.Component.page
{

XData Contents [ XMLNamespace "http://www.intersystems.com/zen" ]
{
<page xmlns="http://www.intersystems.com/zen">
  <tablePane
    id="tp"
    sql="select ID,'Western branch' Branch,{'2021-03-15'&quot;Date&quot;,35 Suma
    union
    select 2,'Eastern branch',{'2020-12-11'},37"
  />
  <button caption="Row unselect" onclick="zenPage.rowUnselect()"/>
</page>
}

ClientMethod rowUnselect() [ Language = javascript ]
{
  var tp=zen('tp');
  row=tp.selectedIndex;

  if (tp.rowSelect && row>=0) {
    var old=tp.enableToggleSelect;

    tp.enableToggleSelect=true;
    tp.selectRow(row);
    tp.enableToggleSelect=old;
  }
}

}

I also will insert my five kopecks.

  • The %Library package also includes stream classes, but those are deprecated. The class library includes additional stream classes, but those are not intended for general use.
    Working with Streams
    I have %Stream.FileCharacter was an order of magnitude faster than %[Library.]File
  • If you rewrite the line-by-line reading to read blocks with further parsing of lines, the speed will more increase by an order of magnitude.
     
    Sample
  • On the Internet, you can find a lot of materials about comparing the speed of reading files (in particular CSV) for different programming languages (Python, C/C++, R, C#, Java, etc.), for example (this is machine translation). Often, those who make such comparisons do not always know all these languages equally well, so sometimes casus happen.
     
    Who do you think in the article above was faster when reading 1e7+ lines: Fortran or C++ ?
  • If we approach the issue formally, then the advantage will be given to compiled languages, not interpreted ones, as well as the implementation that uses all the capabilities of the operating system and hardware.