Speed May Kill Your Code

Speed May Kill Your Code

When I started on this internet journey in 2000, I never considered what would happen to our code if things started executing faster than milliseconds. Nearly 100% of the JavaScript code and the corresponding UniBasic code for Universe and Unidata was never envisioned to run in nanoseconds.

One of the many programmers I worked with over the years thought that combining the payroll subroutines and calculations into a single program of about 10K plus lines would make it run faster. The program spent much of its time cycling in and out of memory it was a boatload slower than the original routines that called each other.

Then there was the time I had about 800K of records and the SELECT process on the Unidata database took about 20 minutes in the late 1990's on our 1MB machine. Another example was I had about 400K records that were on a Universe distributed file of 8 parts that took about an hour to select.

Just the other day I had a piece of code in our CRM system that keeps track of what times the customer is open that had stopped working. It was not noticed because the client that used the code was no longer a client and another client asked what was on the hourly tab. The datatable only displayed a single value instead of 7 values for each date.

The first incarnation of this code was written totally in JavaScript about 10 years ago. Since then it has been used for about 7 years and no problems were detected from the client or our QA department. Moreover, when I converted from dhtmlX to the Webix JavaScript library the code was modified to work with the new Webix library. Here is the dhtmlX Code:

grid_4.addRow((new Date()).valueOf(),"Sun,,,,,,,,,,,,,,,,,,,,,,,,","-1");        

Here is the Webix Code with a slight modification of zeroes for all positions of columns that we are adding.

var jsonData = {};	    
for (var k in jsonColumns) {
        var columnName = jsonColumns[k];		
	createJson(jsonData,columnName, "0");
}
$$("Table_4").add({"id":(new Date()).valueOf(),"G4Wk":"Sun",jsonData});        

There are 6 more rows of data that are not shown in the above examples for Mon, Tue, Wed, Thu, Fri, and Sat. The JavaScript "Date valueOf()" method is the problem child here. According to the documentation on the Mozilla site, the data returned from this method is the "...number of milliseconds for this data since epoch..." with one millisecond resolution.

Each execution of the table adds a line with the Date().valueOf() returns the same milliseconds for all seven calls. This means that the code only writes out a single line instead of 7 for each day of the week. Having tested this on Chrome, Firefox, Edge, and Safari and all of them failed because they now run this in under a millisecond.

The solution I came up with is neither elegant nor particularly smart but it works for now.

$$("Table_4").add({"id":(new Date()).valueOf()+1,"G4Wk":"Sun",jsonData});
$$("Table_4").add({"id":(new Date()).valueOf()+2,"G4Wk":"Mon",jsonData});
$$("Table_4").add({"id":(new Date()).valueOf()+3,"G4Wk":"Tue",jsonData});
$$("Table_4").add({"id":(new Date()).valueOf()+4,"G4Wk":"Wed",jsonData});
$$("Table_4").add({"id":(new Date()).valueOf()+5,"G4Wk":"Thu",jsonData});
$$("Table_4").add({"id":(new Date()).valueOf()+6,"G4Wk":"Fri",jsonData});
$$("Table_4").add({"id":(new Date()).valueOf()+7,"G4Wk":"Sat",jsonData});        

In our UniBasic code to get a unique number I have used the following code for Universe and Unidata. Unidata requires that I convert the time to have decimals to match up with how Universe displays the "SYSTEM(12)" command with three decimals.

UTC.TIME = SYSTEM(12)
IF NOT(UNIVERSE) THEN UTC.TIME = OCONV(UTC.TIME,'MD3')
UNIQUE.NO = DATE():UTC.TIME        

This now fails occasionally as well because some of our calls through U2WebLink using UniObjects for Java to get to UniBasic subroutines are happening in under a millisecond and this variable on the last line is not unique. Here is the code that was needed to fix the problem that was again neither elegant nor particularly smart.

UTC.TIME = SYSTEM(12)
IF NOT(UNIVERSE) THEN UTC.TIME = OCONV(UTC.TIME,'MD3')
UNIQUE.NO = DATE():UTC.TIME:RND(1000)        

When I started learning R83 Pick Basic, Prime Information, Universe, Unidata, and all of the others I did not think I would ever have to address speed issues where my code was running too fast. Those days of awe that my code even ran let alone executed faster than a millisecond are now in front of all of us. As I go through the hundreds of thousands of lines of code to make sure I have no other gotchas when the program executes in under a millisecond, I wish you all good hunting.

要查看或添加评论,请登录

Doug A.的更多文章

  • AI Redux

    AI Redux

    A lot of the comments on my prior article had to do with me not asking the right question about the AI engine. There…

    3 条评论
  • MVBasic (UniBasic) and AI

    MVBasic (UniBasic) and AI

    Rocket Software announced during our UniVerse 14.2.

    6 条评论
  • Java Bug or Doug Bug

    Java Bug or Doug Bug

    I have been fighting with EclipseIDE, trying to upgrade from 2024-03, my current base, to 2024-06, 2024-09, and…

    1 条评论
  • Why do UniBasic compiler changes fail?

    Why do UniBasic compiler changes fail?

    From our Rocket Software community forum, one of the participants asked a question. However, since Rocket Software…

  • Dying Rocket Software MV Technologies

    Dying Rocket Software MV Technologies

    In no particular order, these are the last release dates of some of their products that are either on their last legs…

    2 条评论
  • A New XLr8Tool Release

    A New XLr8Tool Release

    When the XML code was removed from XLr8Installer, it was noticed that the columns that displayed the XML data did not…

  • Converting XML to JSON and UniBasic

    Converting XML to JSON and UniBasic

    Our XLr8Tools, built on top of the open-source EclipseIDE plug-in for Universe and Unidata databases, contain a fair…

  • Goodbye to the D3 Database

    Goodbye to the D3 Database

    This was just announced by Rocket Software the owner of the D3 database: "In 2025, we'll make additional enhancements…

  • Making U2 Software Multi-language for the Web

    Making U2 Software Multi-language for the Web

    Around 2004 while trying to get off U2's Redback web software, I developed this crazy idea that our next version of our…

  • U2 an In-Memory Database?

    U2 an In-Memory Database?

    A few years ago, there were some speed issues accessing Universe/Unidata (U2) data. Ideas were bounced around as to…

社区洞察

其他会员也浏览了