Archive

Posts Tagged ‘if statement’

Estimating variance when measuring source

October 8th, 2009 Derek-Jones No comments

Yesterday I finally delivered a paper on if/switch usage measurements to the ACCU magazine editor and today I read about a switch statement usage that if common, would invalidate a chunk of my results. Does anything jump out at you in the following snippet?

switch (x)
   {
   case 1:
             {
             z++;
             ...
             break;
             }
...

Yes, those { } delimiting the case-labeled statement sequence. A quick check of my C source benchmarks showed this usage occurring in around 1% of case-labels. Panic over.

What is the statistical significance, i.e., variance, of that 1%? Have I simply measured an unrepresentative sample, what would be a representative sample and what would be the expected variance within a representative sample?

I am interested in commercial software development and so I have selected half a dozen or so largish code bases as my source benchmark, preferably written in a commercial environment even if currently available as Open source. I would prefer this benchmark to be an order of magnitude larger and perhaps I will get around to adding more programs soon.

My if/switch measurements were aimed at finding usage characteristics that varied between the two kinds of selection statements. One characteristic measured was the number of equality tests in the associated controlling expression. For instance, in:

if (x == 1 || x == 2)
   z--;
else if (x == 3)
   z++;

the first controlling expression contains two equality tests and the second one equality test.

Plotting the percentage of equality tests that occur in the controlling expressions of if-if/if-else-if sequences and switch statements we get the following:

Number of quality tests in controlling expression

Do these results indicate that if-if/if-else-if sequences and switch statements differ in the number of equality tests contained in their controlling expressions? If I measured a completely different set of source code, would the results be very different?

To answer this question a probability model is needed. Take as an example the controlling expressions present in an if-if sequence. If each controlling expression is independent of the others, then the probability of two equality tests, for instance, occurring in any of these expressions is constant and thus given a large sample the distribution of two equality tests in the source has a binomial distribution. The same argument can be applied to other numbers of equality tests and other kinds of sequence.

Number of quality tests in controlling expression, with error bars

For each measurement point in the above plot the associated error bars span the square-root of the variance of that point (assuming a binomial distribution, for a normal distribution the length of this span is known as the standard deviation). The error bars overlap suggesting that the apparent difference in percentage of equality tests in each kind of sequence is not statistically significant.

The existence of some dependency between controlling expression equality tests would invalidate this simply analysis, or at least reduce its reliability. I did notice that in a sequence that containing two equality tests, the controlling expression that contained it tended to appear later in the sequence (the reverse of the example given above). Did I notice this because I tend to write this way? A question for another day.

To if-else-if or if-if, that is the question

August 21st, 2009 Derek-Jones 2 comments

I am currently measuring if-statements, occurring in visible source, that might be mapped to an equivalent switch-statement. The most obvious usage to look for is a sequence of if-else-if statements that all involve the same expression being tested against an integer constant, as in

if (x == 1)
   stmt_1;
else
   if (x == 2)
      stmt_2;
   else
      if (x == 3)
         stmt_3;

Another possible sequence is:

if (x == 1)
   stmt_1;
if (x == 2)
   stmt_2;
if (x == 3)
   stmt_3;

provided all but the last conditionally executed arms do not change the value of the common control variable (e.g., x).

I started to wonder about what would cause a developer to chose one of these forms over the other. Perhaps the if-if form would be used when it was obvious that the common conditional variable was not modified in the conditionally executed arm. This would imply that there would be more statements in the arms of if-else-if sequences than if-if sequences. The following plot of percentage occurrence (over all detected if-else-if/if-if forms) of line number difference between pars of associated if-statements (e.g., when the controlling expression occurs on line x and the following if-statement controlling expression occurs on line x+2 the distance is 2) shows that this is not the case:

Lines between if-statement controlling expressions

Just over a quarter of the arms contain a single statement (or to be exact the code is contained on a single line); this suggests that when using the if-else-if form most developers put the else and if on the same line. At the next distance along the percentage of if-else-if forms is twice as great as the if-if, probably because of else and if appearing on separate lines (as in the introductory example) in one case and less frequently a comment/blank line in the other. Next along, why the big increase in if-if forms? A comment + blank line, or perhaps no comment or blank line but the use of curly brackets (this is too off the track of where I am supposed to be going to investigate).

This morning I realized why the original plot did not look right, one of the data sets was a way off adding to 100%. An updated version has been uploaded.

It turns out that a single statement (or at least a single line) is more common in the if-else-if form, the opposite of what I had expected. At slightly larger distances there are still differences that can be attributed to else and if appearing on separate lines, curly brackets and a comment/blank line, but the effect is not as large as seen in the original, less accurate, plot.

I have a feeling that I ought to say something about the if-else-if form being preferred to the if-if form. One of the forms will have its behavior changed if the common control variable is modified in one of its arms. But is this an intended or unintended behavior? What is the typical characteristic usage of a common control variable, e.g., do they tend to be accessed but not modified in a given function definition? At the moment I see no obvious cost or benefit strongly favoring one usage over the other, so I will remain silent on the issue.

Implementing the between operation

July 30th, 2009 Derek-Jones 5 comments

What code do developers write to check whether a value lies between two bounds (i.e., a between operation)?  I would write (where MIN and MAX might be symbolic names or numeric literals):

   if ( x >= MIN && x <= MAX )

that is I would check the lowest value first. Performing the test in this order just seems the natural thing to do, perhaps because I live in a culture that writes left to write and a written sequence of increasing numbers usually has the lowest number on the left.

I am currently measuring various forms of if-statement conditional expressions that occur in visible source as part of some research on if/switch usage by developers and the between operation falls within the set of expressions of interest. I was not expecting to see any usage of the form:

   if ( x <= MAX && x >= MIN )

that is with the maximum value appearing first. The first program measured threw up seven instances of this usage, all with the minimum value being negative and in five cases the maximum value being zero. Perhaps left to right ordering still applied, but to the absolute value of the bounds.

Measurements of the second and subsequent programs threw up instances that did not follow any of the patterns I had dreamt up. Of the 326 between operations appearing in the measured source 24 had what I consider to be the unnatural order. Presumably the developers using this form of between consider it to be natural, so what is their line of thinking? Are they thinking in terms of the semantics behind the numbers (in about a third of cases symbolic constants appear in the source rather than literals) and this semantics has an implied left to right order? Perhaps the authors come from a culture where the maximum value often appears on the left.

Suggestions welcome.

www.wenn.com
FireStats icon Powered by FireStatswww.tinynibbles.com discount drug propecia

cialis pharmacy

buying generic cialis mexico rx

levitra from canadian pharmacy

order levitra online

cialis professional 100 mg

buy propecia on line

cheap propecia no prescription

bestellen levitra online

lowest propecia prices

china viagra

cheapest prices for viagra

levitra for sale

cialis en mexico

cialis refractory

buy cialis canada

buy fast propecia

generic cialis sale

i need to buy propecia

generic viagra india

buy viagra mexico

cheap canadian viagra

cialis quick shipment

how much is viagra

gele viagra

lowest price on non generic levitra

buy cialis cannada

generic levitra purchase

buy can from i propecia who

once daily cialis

buy viagra on line

best price propecia

get cialis

online pharmacy propecia viagra

buy cialis for daily use

buy generic levitra

how much cialis

buy levitra online from canada

cialis soft pills

best price for propecia

levitra pill

internet pharmacy propecia

cialis vs levitra

canadian healthcare pharmacy

cheap propecia 5mg

cheapest price propecia cheap

buying levitra online

once a day viagra

cialis dosage mg

cheap discount levitra

mexico pharmacy cialis

lowest priced propecia

cialis discount

canada viagra generic

buy generic propecia

cialis in mexico

best price cialis

cheap viagra online

levitra discount

cialis from canada

indian cialis

order viagra or levitra

discount levitra rx

cialis by mail

generic propecia sale

levitra viagra online

cialis one a day

50 mg cialis

brand name cialis

cialis transdermal

levitra 10mg

cheapest propecia prescription

generic propecia effective

cialis cheap us pharmacy

lowest price propecia best

cialis generic 100 mg

cheapest overnight cialis

canada viagra pharmacies scam

canadian viagra 50mg

ordering propecia online

levitra online overnight delivery

buy propecia online prescription

cialis 100 mg generic

cialis and diarrhea

discount us propecia

generic viagra canadian

buying generic propecia

how to get viagra

buy online prescription propecia

buy canada levitra

indian cialis generic

order cheap levitra

canadian viagra and healthcare

buy dosages levitra

genuine cialis pills

canada online pharmacy levitra

cialis woman

generic propecia finasteride

levitra mail order

does generic cialis work

cialis 20 mg

cialis daily dosage pharmacy

buy now propecia

levitra in india

combine cialis and levitra

cialis and canada custom

buying cialis next day delivery

low cost canadian viagra

canadian viagra india

online propecia uk

hydrochlorothiazide cialis

next day viagra

get propecia online pharmacy

buy viagra

buy cialis usa

buy cheap generic propecia

canadian pharmacy cialis

buy viagra without prescription

canada meds viagra

online pharmacy propecia renova

cialis strenght mg

get levitra online

cheap viagra canada or india

levitra cheap fast

buy propecia online from usa pharmacy

mexico levitra

buy viagra china

best price levitra

buy propecia online pharmacy

cialis online

generic viagra made in india

drug generic propecia

levitra online us

buying viagra in canada

cost of propecia

online levitra

cialis for woman

cheap levitra prescription

buy cialis online canada

levitra online

cialis buy overnight

levitra sales uk

buy prescription propecia without

healthcare canadian pharmacy

buy real cialis

generic propecia online pharmacy

buy generic viagra india rx

canada viagra

cialis price

cheap viagra from uk

order cheap propecia

discount cialis india

lowest cost levitra

buy cheap levitra

cheapest viagra

cheap cialis soft

cheap prescription propecia

discount propecia rx

indian viagra

buying propecia online

generic viagra online

canadian pharmacy discount code viagra

cialis fast delivery usa

5 mg original brand cialis

canada online pharmacy propecia

buy cialis fedex shipping

canadian healthcare

female viagra pills

discount generic propecia

mail online order propecia

cialis fast delivery

canada generic propecia

generic propecia alternative

lowest price levitra

buy propecia in the uk

cialis and ketoconazole

canadian propecia rx

bio viagra herbal

natural viagra

buy real viagra online

cost of daily cialis

brand viagra professional

cialis discounts

buy propecia without prescription

overnight delivery cialis

cialis overnight delivery

best price for generic cialis

buy viagra online cheap us

levitra where to buy

buy generic cialis

cialis from mexico

cheapest propecia sale uk

cheap cialis

levitra canadian

how strong is 5 mg of cialis

cheap propecia uk

generic viagra 100 mg

buying cialis

buy propecia canada

generic levitra vardenafil

brand cialis for sale

canadian drugs propecia

levitra online prescription

generic levitra canada

lowest price for propecia

cialis 5 mg buy

order propecia

mail order propecia

buy levitra online viagra

buy levitra us

buy cialis without prescription

levitra online no prescription

cialis professional 20 mg

5 mg daily cialis

cialis 5 mg

overnight delivery viagra

generic viagra made in usa

cialis daily

levitra viagra cialis

levitra buy online

order prescription propecia

getting cialis from canada

order generic levitra

online propecia prescription

cialis next day

generic levitra cheap

cialis 100 mg

discount levitra online

cialis price in canada

buy levitra overnight

buy propecia generic

levitra online sales

cialis price 100 mg

brand viagra over the net

ordering cialis gel

how to buy cialis in canada

how to get cialis in canada

cheap levitra uk

lowest propecia 1 mg

cheapest viagra online

get cialis online

next day delivery cialis

how much does cialis cost

generic levitra overnight delivery

cialis prescription

levitra next day delivery

buying online propecia

cialis profesional

cheap fast levitra

indian generic levitra

buy viagra germany canadian meds

levitra mg

generic viagra canada

canadian healthcare viagra

cheap cialis from india

cialis delivered overnight

cialis on women

cialis to buy

canadian pharmacy

online cialis

low cost levitra

herbal propecia

buy cheap levitra online

levitra low price

can i get viagra in mexico

online ordering propecia

cheap levitra

buying propecia

levitra now online

buy branded viagra

cheap propecia online

cheap levitra tablets

cheap levitra without prescription

cheapest viagra usa

buy 5 mg cialis

lowest propecia prices in canada

buy levitra vardenafil

best way to use cialis

generic cialis soft tabs

low price levitra

order cheapest propecia online

discount levitra purchase

cialis tablets foreign

levitra order prescription

buy propecia prescriptions online

cost of viagra

get levitra

canada propecia prescription

for sale levitra

canadian viagra

cost levitra low

cialis next day delivery

levitra cost

cialis 5 mg italia

cheap order prescription propecia

cialis tablets

buy cheapest propecia

generic cialis next day shipping

ganeric cialis

buy cheap generic levitra