Wednesday, August 17, 2011

HOWTO: Repeat Group CountRows in Details

I had to create a report for a user that provided the number of rows in the group on each row in the group for SSRS 2005. For example, I needed to create a report with first name, last name, and the total count of first names.







fnamelnamecount
AndySmith1
BobSmith2
BobDoe2


The method I ended up using was to create a group to aggregate by first names, then used the CountRows(scope) [NOTE: default table1_Group1] is to count the rows in the group. Finally, delete row [NOTE: not delete group].

Another method is to create the group in the table properties [Groups Tab > Add...]. This is where you can also edit the group list from the above method because you can no longer access it quickly from the table after deleting the row.

Although the second method is cleaner, I left the first method in because that was the process I figured to get it to work then couldn't figure a way to get back to the group list. I just hope this either helps someone or that someone can provide a better process to do the reporting.

Wednesday, August 10, 2011

Research: XMPP

An interesting thought came up about interfacing with Google Talk (aka GTALK or GTALK) and did not realize how much reading is involved. As a person that enjoys to understand things to as fine detailed as possible, I ended up looking into XMPP.

Here's a link to a very detailed information about XMPP:
http://xmpp.org/rfcs/rfc6120.html

There's a lot of information that already exists, there are two main C# projects that keeps coming up with existing code:
MatriX
Jabber-net

MatriX had some EXE that it downloads. The installation requested to install something at which point I cancelled. I was looking for source code, and MatriX appeared to provide source for the client portion but not the XMPP.

Since I was looking for more about code examples of XMPP, I continued to search and found Jabber-net. It's open source included all the communications, easy ZIP file to download, and provides solution for VS 2003 and 2005. There were no problems converting 2005 to VS 2010 after changing the target framework and deleting the VB project. I haven't actually executed the project yet because I've been distracted by other new items.

C#'s square bracket for attributes was something I've seen commonly but haven't actually sat down to learn and understand. This site was rather helpful in understanding that stuff:
csharphelp

I can see some uses, but I am not fully comfortable with its use yet so some more time needs to be spent on that.

Another interesting piece of code I looked into was their own implementation of the doubly linked list. Not exactly sure why they did not use C# System.Collection LinkedList which I am not too familiar with either [I mean C#'s implementation. I know what they are, just haven't had to the need for them in C# projects thus far]. I am going to hazard a guess that C#'s is not doubly-linked.

Either way, it is interesting how they expanded on the Queue class (but in actuality, technically not expand but rather re-implemented it).

Anyways, there is a lot to read and updating myself on. Hopefully it won't distract me too long from other projects. Another item on the horizon is possibly looking into interfacing with twitter (and other similar social networking sites) and facebook (although the latter seems like it would take some work to do anyways). Others just include statement parsing as that keep running into that when I want to interpret incoming messages. And of course, still need to interface with database. So lots still to do.

Tuesday, August 9, 2011

Tools: Unit Testing

I also started using Visual Studio 2010's Unit Testing. It is very similar to NUnit in structure and execution. The nice thing is that it is fully integrated so not compiling and configuration. It is basically just a combination of right-clicks and wizards/context-menus... very intuitive from a tiny experience with NUnit.

It was also nice to make use of some of the material I learned from my research in Unit Testing for a course I took for my graduate program: create a failed test then a working test. The number of test cases does get complicated when dealing with more subjective and unbounded cases (especially when it is the computer that limits the bound). For example, finding the number of digits in a number does not have any extreme bounds. It is limited by the size of the number type or if using string then the amount of memory, not by the criteria of the problem being solved. So except for the obvious 0, 1, -1, some positive value, and some negative value... testing the upper bounds although possible in some cases just seemed a bit awkward. To make it somewhat easier to catch people from actually breaking it, I put in limitations to the values that can be entered.

One of the things I enjoy doing (not good practice) is learning code through trial-and-error in the project. Unit testing makes this even easier in that I can just fidget with different parameters to see make sure that I get the same results back without having to re-enter data each time. Once the test is set up, it is just a matter of right-clicking and run the test. You could even write the code in the unit test but that requires more work later in that I have to then reincorporate it to the main code and write tests again.

Anyways, my quick two cents on this matter.

Benchmarking: Number of digits in a number

I've been curious about benchmarking certain code to see if which methods are better but just never got around to it. This is my first attempt because I wanted to know the efficiency of my method.

The algorithm I am trying to test is to find the number of digits in a number (in my case of type long). Initially I tried to put in some hard-coded "random" numbers and the results came almost eventually with the mathematical approach to be slightly faster. Then I entered some larger numbers and the results became clearer.

Approach #1 (mathematically):
I used division until the value reached 0 to determine the length of the number.

Approach #2 (convert to string):
I used a ToString function to find the length of the string.

Code to Approach #1:
1:   public static int NumberOfDigits(long Number)  
2:      {  
3:        long temp = System.Math.Abs(Number);  
4:        int counter = 0;  
5:        while (temp > 0)  
6:        {  
7:          temp = temp / 10;  
8:          counter++;  
9:        }  
10:        if (Number == 0) { counter = 1; }  
11:        return counter;  
12:      }  


Code to Approach #2:
1:      public static int NumberOfDigits_V2(long Number)  
2:      {  
3:        string temp = System.Math.Abs(Number).ToString();  
4:        return temp.Length;  
5:      }  


Benchmark includes 105 different values including negative, zero, and positive values. Running 10,000,000 calculations of the preset 105 values.

The results in milliseconds:
#1
1387.0793
1733.0992

#2
1416.081
1806.1033

#3
1422.0813
1804.1032

Conclusion, converting to string takes longer than the actual calculations but both takes less than a fifth of a millisecond. The code for the string conversion is much simpler and may be preferable for more readability.

Tests can be run from http://www.dlastlee.com and type in run test1