Thursday, July 29, 2010

Back to Basics: C Basic Types and Their Representation in Memory

Great lectures Stanford put online http://www.youtube.com/watch?v=jTSvthW34GU 
The linked lecture explains very nicely two's complement are 15:00. Other great lectures in this series called Programming Paradigm.

Something I liked is that the lecturer explained why two's complement system is used to represent negative numbers. In short basically it makes basic operation like addition and subtraction really easy for hardware and thus fast. Examples are given in the lecture to illustrate.
see near the end of the lecture where representing floating-point numbers is discussed.
#include 
int main()
{
 int i = 37;
 float f = * (float *) &i;

 printf("%f\n",f);

 return 0;
}
You'll see why the code above does not print what one thinks it might print it first glance. By dereferencing a float point cast from the address of i, we're causing f to have the same pattern of 37 when interpreted as int but not the value 37.




Wednesday, July 28, 2010

Open . == Start .

If you're used to Windows command shell cmd.exe, you probably execute start . to start explorer with current directory; very nice saves a few seconds from the tedious navigation by hand.

In Mac OS X, the same thing can be done with open . or open /some/directory. Very handy.

Some variations of the open command

open . -g  : opens finder in the background

open -e filename : open filename with textedit { -t for default text editor and using -a you can specify application }

open filename : will open with default app

obviously the man pages have much more ...

Thursday, July 22, 2010

Why my Cache´/ ODBC query is awfully slow?

It's not supposed to be this slow. The query is simple, optimized, performs great in different environment /severs or it just does not make it can be this slow. We're talking about an hour to hour.5 of execution time. It just does not make sense; it was working fine a while back. What's changed?

Suddenly you visit a tab in ODBC Data Source Administrator dialog box that you rarely (maybe) check. The tracing tab. Yes the tracing tab. Tracing was active. Some drivers like Cache odbc driver gives its own logging/tracing; that can slow queries. Because the query itself; its result and statistics about it and its transmission over the network are logged to file. Sometimes every row retrieved is analyzed and logged. This surly slows down your query. You use to debug issues. you if forgotten active, it can be a hidden cause of why queries take too long to come back with results.

Something happened to me a long time ago but wanted to record it here. Sometimes a problem seems daunting, sophisticated etc but the fix is awfully simple.

Thursday, July 8, 2010

JavaScript Performance

Playing with client-side filtering. If we have a table with a number of rows, the goal is to allow a user type some text and if particular columns contain the typed text they will be left shown and the rest will be hidden (css display:none).



COL1COL2COL3
Value 1 to filter Value2 to filterValue3

First Attempt


We can have thousands of rows in a table. So iterate over all rows. for each row get its cells and then use indexOf on the cell innerText property to see if the string being searched for is in the table.

function FilterTable(tableName, text){
 var table = document.getElementById(tableName);
 var trs = table.getElementsByTagName('tr');
 var tds = [];
 if (text == '') {
 
  var tds = [];
  for (var i = 0; i < trs.length; i++) {
   trs[0].style.display = '';
  }
 }
 else {
  for (var i = 1; i < trs.length; i++) {
   trs[i].style.display = none;
   tds = trs[i].getElementsByTagName(td);
   index = tds[0].innerText.indexOf(text) + tds[1].innerText.indexOf(text);
   if (index > -1) {
    trs[i].style.display = '';
   }
  }
 }
}

I was only filtering by first and second column so I did not really iterate over every column.
The interesting thing is that this approach has a terrible terrible performance on large number of rows > 2500. The surprising thing is that even though performance is generally awful; it's even worse in Chrome than in IE8!!


Second Attempt


Playing with the above code, I noticed that the most expensive operation was actually not the indexOf call (although I didn't really use the chrome/ie8 profiler to verify this). The most expensive operation is accessing the DOM and changing properties. Namely this:
trs[i].style.display = 'none';

So in my second attempt I made some important changes:
  • in the search loop I instead saved the indices of the table rows whose cells contain the text for which the code is filtering the table. I saved those indices to an array. Later in the code I iterated quickly over the rows and set them all to hidden and one more pass over of the RowsToBeShown array and set those rows only to be visible.

  • The second change: instead of calling indexOf on two cells, I concatenated (at the server side) the contents of both cells and put them in the title property of the first cell. Now I have to call indexOf just once.

  • Another change is that I use jquery this time to select the cells to be filtered. I set the class name of the those cell to "FilterTarget".


function NewFilterTable(tableName,text)
{
  text = trim(text.toUpperCase());
     var table = document.getElementById(tableName);
     var tds = [];
     var none = 'none';
     var showthese = [];

     var j = 0;
     var td = 'td';
     var index = -1;
     if (text == '') {
         var trs = table.getElementsByTagName('tr');
         for (var i = 1; i < trs.length; i++) {
             trs[i].style.display = '';
         }
     }
     else {
         tds = $(".FilterTarget");         
         for (var i = 0; i < tds.length ; i++) {
             if (
             tds[i].title.indexOf(text) > -1 ) {
                 showthese.push(i);
             }
         }
     }
     for (var i = 0; i < tds.length; i++) {
         tds[i].parentNode.parentNode.style.display = none;
     }
     for (var i = 0; i < showthese.length; i++) {
         tds[showthese[i]].parentNode.parentNode.style.display = '';
     }
     return true;
}
Although I am still iterating twice over the tds array, moving the DOM accessing code out of the search helped performance big time. Withe first approach, Chrome would choke when the filter function is called and give the user a choice to stop executing the JavaScript code. IE does take very long time relatively speaking to run through the filtering code. With the seconds approach and with the same number of rows in the table, both IE8 and Chrome take about 3-5 seconds to run the function.
I am sure that are better ways/well-tested libraries that will do the filter much better but it's rather interesting what moving things around can do to performance. I will see what jQuery API can do here and also jQrid plugin for jQuery is awesome by the way for showing tables and doing an amazing number of things http://www.trirand.com/blog/
here the trim function


function trim(text) {
     return text.replace(/^\s+|\s+$/, '');
 }



I changed some variable names and minor things while typing up this post, so there might some js errors

Saturday, July 3, 2010

css dropdown menu

just a very simple css styling with a bit of javascript code to create a drop down list with one just one level of nesting for now. will put here for reference
<script type="text/javascript">
function countdown (num) {
alert('testing ...');
        for (var i = 0; i <= num; i += 1) {
           // setTimeout(function(){doAlerts(num,i)} , i * 1000);
         setTimeout('doAlerts('+num+','+i+');' , i * 1000);

}
}
//
function doAlerts(num,i)
{
 alert(num - i);
}
    //countdown(5);
var g_display = 'block';
function toggleSubMenu(sender)
{
 var display = sender.getElementsByTagName('ul')[0].style.display;
//alert(display);
 if(display != null && display != '')
{
  g_display = display ;
  sender.getElementsByTagName('ul')[0].style.display = '';
}
else
{
  sender.getElementsByTagName('ul')[0].style.display =  g_display;
}
}
</script>
<style type="text/css">
.menu
{
}
.menu > li
{
 float:left;
 margin-right:1px;
 padding: 0px 5px 0px 5px;
 list-style-type:none;
 border: solid 1px #c4c4c4;
 background-color:#c1c1c1;
 width:120px;
}
.menu > li:hover
{
 /* background-color : blue; */
}
ul
{
 padding: 0px 5px 0px 5px;
 list-style-type:none;
 border: solid 1px #c4c4c4;
}
ul li ul
{
 display:none;
 padding: 0px 0px 0px 0px;
 list-style-type:none;
 border: solid 1px #c4c4c4;
}
ul li ul li
{
 display:block;
}
ul li ul li:hover
{
 background-color:#549ade;
}
</style>
 <input onclick="countdown(5);" type="button" value="Do It" />


<ul class="menu">
<li onmouseout="toggleSubMenu(this);" onmouseover="toggleSubMenu(this);">
        Thing 1
        
<ul>
<li>Subthing A</li>
<li>Subthing B</li>
<li>Subthing C</li>
</ul>
</li>
<li onmouseout="toggleSubMenu(this);" onmouseover="toggleSubMenu(this);">
        Thing 2
        
<ul>
<li>Subthing D</li>
<li>Subthing E</li>
<li>Subthing F</li>
</ul>
</li>
<li onmouseout="toggleSubMenu(this);" onmouseover="toggleSubMenu(this);">
        Thing 3
        
<ul>
<li>Subthing G</li>
<li>Subthing H</li>
<li>Subthing I</li>
</ul>
</li>
</ul>