CPU clock counting

Today I want to do some Implementation on code, to see it its actually improving on speed when executing.  Below I wrote a test program which i can place section of code into, and use the RDTSC command to read the clock ticks elapsed.  I know its not yet 100% perfect, i’m not even sure if i’m getting the double or quad value return correctly since its volitile asm code.  I’m simply reading the clock ticks before and after the process code runs.  Its relative to the system since i’m in a windowed environement. there will be other thigns happening in other processes. The only way i would be able to see ONLY the codes instruction clocks then I would need to only one process running on the machine when this program is called. maybe something doe a DOS environment would be made.  here is the code.

/*compile like this: gcc clock_test.c

if you want to try this in windows, replace sleep with Sleep and use the windows.h instead of unistd.h header

*/

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>

double rdtsc()
{
double ret;

asm volatile ( “RDTSC” : “=A”(ret) );
//asm volatile ( “CPUID\n\trdtsc” : “=A”(ret) );

return ret;
}

int main(int argc, char* argv[])
{
double aa,bb,c;
int a,b, j;
c=atol(argv[1]);
for (j=0;j<200;j++) {
a=clock(); aa=rdtsc();

sleep(c); //put your timed code in here instead of sleep.

b=clock(); bb=rdtsc();
printf(“%d %d\n”,b-a, bb-aa);}
return 0;
}

 

Changes to this site

Today is a new day.
I’ve changed the name of the Word press site to “the Casted”. I did that because most of what I like to think about it of some form of a ‘CAST’
Here are some examples of how I’ll use the word:

Cast – as in, the collection of people in a movie or theatrical production. It can also be the collection of people who are assigned a particular job, like the cast of a crew on a ship, or a team of people working on a project.

Casted – Someone who has been removed from this group, usually out, because they’re different from the rest of the people in the group. Outcast. like someone who has different beliefs or different opinions from the others in the group, or is not liked by the other members of the group so that person is removed, or casted out.

Cast – as in programming, where in C, and C++, a type of variable that is declared as one type is converted to another type so that it can be used for its value rather than worrying about the type of variable it is. an example is an integer variable, casted as a double variable, because a function that requires parameters need to be used as doubles. the value of the integer is put in the double variable without loss of meaning. the size of the variable will differ though. INT is usually much smaller in byte size than a DOUBLE.

CAST – as in, something thats used to fix broken bones in arms or limbs.

CAST – verb, like throwing something like a fishing pole lure into the water, to catch fish. this is symbolic in the act of fishing, where someone is trying to get the fish, to bite at the bait, baiting – to get someone to come out of the water and take in the idea of the free food. it ends up catching the fish, or believer, and then putting them away with what the fisherman wants to do with the fish.

Lastly, CAST, as in light CASTING a shadow. without darkness, there is always light. Casts of shadows and of doubts are always there because there is no dark without light. I take that to heart because there is always two sides of the coin. the good with the bad.

Hartmann mask revisited

After looking at OpenScad, I feel like I needed to make another Hartmann mask with the 3d printer.  Here is the script to generate the STL file.

//Hartmann Mask
module triangle(side)
{
 $fn=24;
 hull(){
 
 rotate([0,0,0]) translate ([side,0,0])
 cylinder(side,1,1);
 rotate([0,0,120]) translate ([side,0,0])
 cylinder(side,1,1);
 rotate([0,0,240]) translate ([side,0,0])
 cylinder(side,1,1);
}}
color("orange")
difference()
{ 
 { //main cap
 translate ([0,0,-7])
 difference(){
 cylinder(7,43,43);
 translate([0,0,.01])
 cylinder(9,40.01,40.01);
 }
 }
 { 
 translate([0,0,-7.5])
 {
 rotate([0,0,0]) translate ([23,0,0])
 //cube([15,15,3], center=true);
 triangle(8);
 rotate([0,0,120]) translate ([23,0,0])
 //cube([15,15,3],center=true); 
 triangle(8);
 rotate([0,0,240]) translate ([23,0,0])
 //cube([15,15,3],center=true); 
 triangle(8);
 }
 }
}

And here is what the object looks like:

mask

As you can see, I changed the script to use triangles instead of squares.  I dont know what this will look like becuase I only experimented with two round holes on my first mask.  This one will be interesting.  It still needs corrections on the size of the lip, and the depth of the cover.  I’m assuming 2mm is still flexible, but at 3mm the body will be rigid.   I’m hoping that the cutout shapes will focus into easy to see images for focus.

My original article describes the principals about this mask.

Let me know if you make one of these, I’m interested in how yours turns out.

Thanks for reading!

Openscad

OpenScad is something that I started to play with. Here is a script I made to make a hinge shape. OpenScad is very easy to understand and can change quickly to adapt to whatever geometric shape you need.

I’m planning to use this for the hinge on my binocular mount I made a few years earlier. I think there is a problem with how it is designed right now.  The hinge is not aligned properly with the binoculars, and at high magnification, there is a problem at focus.  I think with a more precise fit hinge at the mirror, It will help with the alignment problem.  I will also redesign the connection between the base and the binocular too.  I need more degrees of freedom for adjustment.  This hinge is the first step in designing a more accurate alignment for the mirror base.

_MG_8680

I will try to print this out on the 3D printer.  This will be the first short segment, just to get it printed and checked out.  It will be a longer segment, and probably taller once I figure how it will fit on the base.  The great thing about OpenScad is that you can change the parameters like the size of the circle, and the distance between the hinges and it will automatically figure itself into the bigger object.  I didnt have to change lines and points, I only had to change formulas and how its used in the SCad format.  It allows the creative process to flow while I design a shape that I can work with.  I will have to print this out on low resolution PLA first.  I have ABS but I cant control the warping yet.  I like PLA becuase its very rigid and it is easier to work with.  I want to put in ABS later when I am commited to the final dimensions.  ABS is supposed to be more durable than PLA in outside environments.  But who knows, PLA might be the final material becuase this isnt going to move much, and I’ll be using it outside during the night.  We’ll fine out when we get there.

Here is the script, and let me know if you have any questions, and let me know if you have any ideas.  I’m open to any suggestions on how to make the binocular mount better to use.

Thanks for reading,
hinge

module hinge(side, radius, depth){
$fn=24;
 rotate([-90,0,0]) translate ([0,-20,0]) 
{difference(){
 union(){
 difference(){
 hull(){
 translate([-(side/2),0,0])
 cube([side,side,depth]);
 cylinder(r=radius,h=depth);
 }
 translate([0,0,-1])
 cylinder(r=2,h=depth*2);
 }
 translate([-(side/2),6,0]) cube([side,side,depth*2]);
 }
{
 //color("red")
 translate([0,0,depth])
 cylinder(r=radius+.2, h=depth+.1);
 }
}
}
}

 //hinge(14,8.5,10);
for (a=[1:2])
 {
 translate([0,a*10,0])
 hinge(11,8.9,5);
 }

 

Pyodbc to do simple things…

Python ODBC is still awesome.

I’d like to point out that I am not a DBA or an Administrator. I do, however, work as a programmer and do lots of database related things.
I’ve recently come to a problem which I can solve quickly without much programming or using third-party software to check on database table objects. I use Pyodbc to export two table structures in MSSQL. They can be different tables in different databases as long as Pyodbc can make an ODBC connection to the tables. I use a third-party program to compare text files to point out differences in the files made. I’m basically doing a side by side comparison on all Triggers, Stored Procedures, and Table structures on two different databases. this has helped me to quickly identify if i am missing an upgrade or missing an installation from some point. I’m sure this is the most basic feature used on most database tools. I found it so useful that I was able to do a test database upgrade in a few days without any knowledge of the databases history. It gives me a tool to quickly point out what missed the scripts.

it has some drawbacks, like you have to know what tables to compare with, and the command line parameters you need to use your file comparison tool, but I think that’s as simple as it gets before buying a dedicated tool just for the purpose.

I like Python with its simplicity and utility.

below is an example script I used for the trigger comparison.
note that I have a trusted mssql connection to the DB. Also, I have installed PYOdbc with Python 2.6 (I believe, but it shouldn’t look much more different if it were another version). I also use the third-party program that compares text files called WinDiff. This is a very nice program, if I knew the link I’d tell you where to go, but Google it and you should find it quickly.

here is a screen shot as well of the comparison of a trigger.

windiff

#compare two datsbases with winmerge. output is from sql query taken from two databases to compare.
 import pyodbc
 import os
#-------stage one: EXPORT tables format---------------
 print 'DATABASE COMPARISON '
 #server=raw_input("SERVER: ")
 #database = raw_input("DATABASE: ")
 #connection_string = 'DRIVER={SQL Server};SERVER=x;DATABASE=x' #master
sp_list =[
 'trg_d_vmi_consumption_det',
 'trg_d_vmi_inv_adjustments'
 ]
##part 1, export copy source ##
connection_string = 'DRIVER={SQL Server};SERVER=y;DATABASE=y' #COPY
output = open(r'copy.txt','w')
 connection = pyodbc.connect(connection_string)
 cursor = connection.cursor()
 output_str = ''
 output.writelines(connection_string + '\n')
 for sp in sp_list:
 try:
 cursor.execute('sp_helptext ' + str(sp))
 results = cursor.fetchall()
 for eachrow in results:
 output_str = eachrow.Text
 output_str = output_str.replace(u'\xfb','')
 output_str = output_str.replace(u'\u2013','')
 output_str = output_str.replace(u'\u2018','')
 output_str = output_str.replace(u'\u2019','')
 output_str = output_str.replace(u'\n','')
 output.write(output_str)
 except:
 print sp + " tr doesnt exist"
 output_str = sp + " tr doesnt exist"
 output.write(output_str+'\n')
 cursor.close()
 connection.close()
 output.close()
##part 2, export master source ##
 connection_string = 'DRIVER={SQL Server};SERVER=z;DATABASE=z' #COPY
 output = open(r'master.txt','w')
 connection = pyodbc.connect(connection_string)
 cursor = connection.cursor()
 output_str = ''
output.writelines(connection_string + '\n')
 for sp in sp_list:
 try:
 cursor.execute('sp_helptext ' + str(sp))
 results = cursor.fetchall()
 for eachrow in results:
 output_str = eachrow.Text
 output_str = output_str.replace(u'\xfb','')
 output_str = output_str.replace(u'\u2013','')
 output_str = output_str.replace(u'\u2018','')
 output_str = output_str.replace(u'\u2019','')
 output_str = output_str.replace(u'\n','')
 output.write(output_str)
 except:
 print sp + " tr doesnt exist"
 output_str = sp + " tr doesnt exist"
 output.write(output_str+'\n')
 cursor.close()
 connection.close()
 output.close()
#-------(END) stage one: EXPORT tables format---------------
 #-------stage two: COMPARISON---------------
path = r'C:\program files\winmerge\winmerge.exe'
 arg0 = ''
 arg1 = r"master.txt"
 arg2 = r"copy.txt"
 os.execl( path, arg1, arg1, arg2)

Have you ever taken apart a light bulb?

Things sound so simple at first glance.  But have you ever tried this?  You can end up with a lot of parts for your components bin.  I’m not talking about regular old incandescent ones though.  I mean the new Compact fluorescent bulbs.  The old ones are simple and you probably couldn’t use anything once they blow up.  But these mysterious bulbs have a lot of things shrouded inside.  I couldn’t pass up the temptation when my wife was about to throw away a bulb.

These were GE 10w CFC bulbs.
First break off the glass components and get rid of that.  I clamped mine down in a vise, covered it with a towel and crushed the glass off.  I didn’t want to deal with trying not to break the glass while I tried to open up the case. After reading up on the internals of a CF bulb, I recommend not breaking the glass and avoiding this completely.  There is mercury vapor inside and is very toxic.  Keep the kids away from this if it ever does break.  Don’t let animals sniff around either.  Mercury is toxic and can cause brain damage.

With a vise grip and pliers I wrenched off the metal sleeve that’s the bulb socket.  It’s a sleeve of aluminum which is the screw part of the bulb.  Inside you will see a plastic housing and maybe some vent holes.  In the holes you could see the components.  I saw a huge electrolytic capacitor and several thinner film capacitors.
Next with a Dremel tool, cut away at the plastic case.  Just cut enough to be able to safely snap away the casing.  Remember there is glass on this thing.  Wear eye protection and safely pry open the case.  I had a circuit board attached by 4 wires to the bulb unit, and a red and black wire going to the socket connection.  The circuit however had many things on it.  I thought there was a transformer on it too but I was wrong because it only had two leads and the labeling indicates a coil of about .140 H.  I will need to test this.  The film capacitors look nice too but their markings don’t say much, and actually they’re marked with OHMS and KV.  I’m a little confused.  I was able to recognize the diodes and the electrolytic capacitor.  There are also some other parts which are probably fuses.  I should have checked the markings on the PCB because they show what component types they are.  Anyways, I was so excited to see free components and I was de-soldering away.

Next time you’re throwing away a CFC bulb, go ahead and take a look inside.  You could hit jackpot and get a bunch of free stuff.  Save you a trip from going to Radio Shack or Fry’s.

pictures will follow,  but not of the process ,

Thanks for reading!

Query Analyzer

How many times do you need to debug a program in SQL but the machine you want to check on doesnt have MSSQL client?

I used a command line query tool to do this, using some libraries found on the web. But the problem is, people just dont like dos/command shells.

I found this cool program from Query Express.

Joseph Albahari made a nice interface and I just wanted to point others to his site, which I found by just googling “free query analyzer”. I’m in no way affliated with Albahari, but I do use his program to help with database problem solving.

I hope you find his resource useful.