Purpose: Testing various implementations of a lookup table
Author: Chris Wallace
(for $s in 1 to 200
for $step in ("A","B","C","D","E","F","G")
return local:MidiNote(
<pitch>
<step>{$step}</step>
<octave>5</octave>
</pitch>)
)[1]
| Task | Null loop |
|---|---|
| XQuery |
declare function local:MidiNote($thispitch as element(pitch) ) as xs:integer {
81
} ;
|
| Output |
81
|
| Elapsed Time (ms) | 59 |
| Task | nested if-then-else expression |
|---|---|
| XQuery |
declare function local:MidiNote($thispitch as element(pitch) ) as xs:integer
{
let $step := $thispitch/step
let $alter :=
if (empty($thispitch/alter)) then 0
else xs:integer($thispitch/alter)
let $octave := xs:integer($thispitch/octave)
let $pitchstep :=
if ($step = "C") then 0
else if ($step = "D") then 2
else if ($step = "E") then 4
else if ($step = "F") then 5
else if ($step = "G") then 7
else if ($step = "A") then 9
else if ($step = "B") then 11
else 0
return 12 * ($octave + 1) + $pitchstep + $alter
} ;
|
| Output |
81
|
| Elapsed Time (ms) | 171 |
| Task | nested if-then-else expression - - sequence used for $alter |
|---|---|
| XQuery |
declare function local:MidiNote($thispitch as element(pitch) ) as xs:integer
{
let $step := $thispitch/step
let $alter :=xs:integer(($thispitch/alter,0)[1])
let $octave := xs:integer($thispitch/octave)
let $pitchstep :=
if ($step = "C") then 0
else if ($step = "D") then 2
else if ($step = "E") then 4
else if ($step = "F") then 5
else if ($step = "G") then 7
else if ($step = "A") then 9
else if ($step = "B") then 11
else 0
return 12 * ($octave + 1) + $pitchstep + $alter
} ;
|
| Output |
81
|
| Elapsed Time (ms) | 174 |
| Task | Define table as a sequence of elements |
|---|---|
| XQuery |
declare variable $noteStep :=
(
<note name="C" step="0"/>,
<note name="D" step="2"/>,
<note name="E" step="4"/>,
<note name="F" step="5"/>,
<note name="G" step="7"/>,
<note name="A" step="9"/>,
<note name="B" step="11"/>
);
declare function local:MidiNote($thispitch as element(pitch) ) as xs:integer
{
let $alter := xs:integer(($thispitch/alter,0)[1])
let $octave := xs:integer($thispitch/octave)
let $pitchstep := xs:integer($noteStep[@name = $thispitch/step]/@step)
return 12 * ($octave + 1) + $pitchstep + $alter
} ;
|
| Output |
81
|
| Elapsed Time (ms) | 272 |
| Task | As above but loop invariant removed |
|---|---|
| XQuery |
declare variable $noteStep :=
(
<note name="C" step="0"/>,
<note name="D" step="2"/>,
<note name="E" step="4"/>,
<note name="F" step="5"/>,
<note name="G" step="7"/>,
<note name="A" step="9"/>,
<note name="B" step="11"/>
);
declare function local:MidiNote($thispitch as element(pitch) ) as xs:integer
{
let $alter := xs:integer(($thispitch/alter,0)[1])
let $octave := xs:integer($thispitch/octave)
let $name := $thispitch/step
let $pitchstep := xs:integer($noteStep[@name =$name]/@step)
return 12 * ($octave + 1) + $pitchstep + $alter
} ;
|
| Output |
81
|
| Elapsed Time (ms) | 203 |
| Task | Define table as a nested element |
|---|---|
| XQuery |
declare variable $noteStep :=
<steps>
<note name="C" step="0"/>
<note name="D" step="2"/>
<note name="E" step="4"/>
<note name="F" step="5"/>
<note name="G" step="7"/>
<note name="A" step="9"/>
<note name="B" step="11"/>
</steps>;
declare function local:MidiNote($thispitch as element(pitch) ) as xs:integer
{
let $alter := xs:integer(($thispitch/alter,0)[1])
let $octave := xs:integer($thispitch/octave)
let $pitchstep := xs:integer($noteStep/note[@name = $thispitch/step]/@step)
return 12 * ($octave + 1) + $pitchstep + $alter
} ;
|
| Output |
81
|
| Elapsed Time (ms) | 280 |
| Task | As above but loop invariant removed |
|---|---|
| XQuery |
declare variable $noteStep :=
<steps>
<note name="C" step="0"/>
<note name="D" step="2"/>
<note name="E" step="4"/>
<note name="F" step="5"/>
<note name="G" step="7"/>
<note name="A" step="9"/>
<note name="B" step="11"/>
</steps>;
declare function local:MidiNote($thispitch as element(pitch) ) as xs:integer
{
let $alter := xs:integer(($thispitch/alter,0)[1])
let $octave := xs:integer($thispitch/octave)
let $name := $thispitch/step
let $pitchstep :=xs:integer($noteStep/note[@name = $name]/@step)
return 12 * ($octave + 1) + $pitchstep + $alter
} ;
|
| Output |
81
|
| Elapsed Time (ms) | 206 |
| Task | As above but loop invariant removed and no explicit casting |
|---|---|
| XQuery |
declare variable $noteStep :=
<steps>
<note name="C" step="0"/>
<note name="D" step="2"/>
<note name="E" step="4"/>
<note name="F" step="5"/>
<note name="G" step="7"/>
<note name="A" step="9"/>
<note name="B" step="11"/>
</steps>;
declare function local:MidiNote($thispitch as element(pitch) ) as xs:integer
{
let $alter := ($thispitch/alter,0)[1]
let $octave := $thispitch/octave
let $name := $thispitch/step
let $pitchstep :=$noteStep/note[@name = $name]/@step
return 12 * ($octave + 1) + $pitchstep + $alter
} ;
|
| Output |
81
|
| Elapsed Time (ms) | 211 |
| Task | Define table as a nested element |
|---|---|
| XQuery |
declare variable $noteStep :=
<steps>
<note><name>C</name><step>0</step></note>
<note><name>D</name><step>2</step></note>
<note><name>E</name><step>4</step></note>
<note><name>F</name><step>5</step></note>
<note><name>G</name><step>7</step></note>
<note><name>A</name><step>9</step></note>
<note><name>B</name><step>11</step></note>
</steps>;
declare function local:MidiNote($thispitch as element(pitch) ) as xs:integer
{
let $alter := xs:integer(($thispitch/alter,0)[1])
let $octave := xs:integer($thispitch/octave)
let $name := $thispitch/step
let $pitchstep := xs:integer($noteStep/note[name = $name]/step)
return 12 * ($octave + 1) + $pitchstep + $alter
} ;
|
| Output |
81
|
| Elapsed Time (ms) | 262 |
| Task | Define table in database |
|---|---|
| XQuery |
declare variable $scale := doc("/db/Wiki/Music/notemap.xml")//note;
declare function local:MidiNote($thispitch as element(pitch) ) as xs:integer
{
let $alter := xs:integer(($thispitch/alter,0)[1])
let $octave := xs:integer($thispitch/octave)
let $pitchstep := xs:integer($scale[@name = $thispitch/step]/@step)
return 12 * ($octave + 1) + $pitchstep + $alter
} ;
|
| Output |
81
|
| Elapsed Time (ms) | 313 |
| Task | Define table in database - loop invariant removed |
|---|---|
| XQuery |
declare variable $scale := doc("/db/Wiki/Music/notemap.xml")//note;
declare function local:MidiNote($thispitch as element(pitch) ) as xs:integer
{
let $alter := xs:integer(($thispitch/alter,0)[1])
let $octave := xs:integer($thispitch/octave)
let $name := $thispitch/step
let $pitchstep := xs:integer($scale[@name = $name]/@step)
return 12 * ($octave + 1) + $pitchstep + $alter
} ;
|
| Output |
81
|
| Elapsed Time (ms) | 229 |
| Task | Computed from codepoint |
|---|---|
| XQuery |
declare variable $noteStep := (9,11,0,2,4,5,7);
declare function local:MidiNote($thispitch as element(pitch) ) as xs:integer
{
let $alter := xs:integer(($thispitch/alter,0)[1])
let $octave := xs:integer($thispitch/octave)
let $index := string-to-codepoints($thispitch/step) - 64
let $pitchstep :=xs:integer($noteStep[$index])
return 12 * ($octave + 1) + $pitchstep + $alter
} ;
|
| Output |
81
|
| Elapsed Time (ms) | 140 |