{"id":76,"date":"2008-08-12T14:20:07","date_gmt":"2008-08-12T19:20:07","guid":{"rendered":"http:\/\/www.amibroker.com\/kb\/?p=76"},"modified":"2018-12-11T02:44:29","modified_gmt":"2018-12-11T01:44:29","slug":"afl-execution-speed","status":"publish","type":"post","link":"https:\/\/www.amibroker.com\/wordpress\/kb\/2008\/08\/12\/afl-execution-speed\/","title":{"rendered":"AFL execution speed"},"content":{"rendered":"

NOTE: Benchmarks and timings given below are little outdated because article was written back in 2008. Today’s CPUs are faster and memory bandwidth is also higher.<\/em><\/p>

AmiBroker Formula Language (AFL) thanks to its array processing<\/a> model is able to run at the same speed as code written in assembler (i.e. machine code). The following article explains how.<\/p>

AFL runs with native assembly speed when using array operations.
A simple array multiplication like this:<\/p>X <\/span>= <\/span>Close  <\/span>* <\/span>H<\/span>; <\/span>\/\/ array multiplication (each array element is multiplied<\/code>

gets compiled by AmiBroker to just 8 assembly instructions<\/strong>:<\/p>

loop:
mov edx,dword ptr [esp+58h]
inc esi ; increase counters
add eax,4
cmp esi,edi
fld dword ptr [edx+esi*4-4] ; get element of close array
fmul dword ptr [eax+ecx-4] ; multiply by element of high array
fstp dword ptr [eax-4] ; store result
jl loop ; continue until all elements are processed
<\/code><\/p>

As you can see there are three 4 byte memory accesses per loop iteration (2 reads each 4 bytes long and 1 write 4 byte long)<\/p>

With such tight loop, single processor core running an AFL formula is able to saturate memory bandwidth in majority of most common operations\/functions if total array sizes used in given formula exceedes DATA cache size.<\/p>

On my (2 year old) 2GHz Athlon x2 64 single iteration of this loop takes 6 nanoseconds<\/strong> (see benchmark code below). 6 nanoseconds to process one bar of data, or 166 million bars per second. So, during 6 nanoseconds we have 8 byte reads and 4 byte store. Thats (8\/(6e-9)) bytes per second = 1333 MB per second read and 667 MB per second write simultaneously i.e. 2GB\/sec combined !<\/p>

Now if you look at memory benchmarks<\/a> you will see that 2GB\/s is the limit of system memory speed on Athlon x64 (DDR2 dual channel)
And that’s considering the fact that Athlon has superior-to-intel on-die integrated memory controller (hypertransfer)<\/p><\/span>\/\/ benchmark code
\/\/ for accurrate results run it on LARGE arrays -
\/\/ intraday database, 1-minute interval, 50K bars or more)
<\/span>GetPerformanceCounter<\/span>(<\/span>1<\/span>);
for(<\/span>k <\/span>= <\/span>0<\/span>; <\/span>k <\/span>< <\/span>1000<\/span>; <\/span>k<\/span>++ )  <\/span>X <\/span>= <\/span>C <\/span>* <\/span>H<\/span>
<\/span>"Time per single iteration [s]="<\/span>+<\/span>1e-3<\/span>*<\/span>GetPerformanceCounter<\/span>()\/(<\/span>1000<\/span>*<\/span>BarCount<\/span>); <\/code>

Only really complex operations that use *lots* of FPU (floating point) cycles such as trigonometric (sin\/cos\/tan) functions are slow enough for the memory to keep up.<\/p>","protected":false},"excerpt":{"rendered":"

NOTE: Benchmarks and timings given below are little outdated because article was written back in 2008. Today’s CPUs are faster and memory bandwidth is also higher.AmiBroker Formula Language (AFL) thanks to its array processing model is able to run at the same speed as code written in assembler (i.e. machine code). The following article explains […]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[2],"tags":[53],"_links":{"self":[{"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/posts\/76"}],"collection":[{"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/comments?post=76"}],"version-history":[{"count":2,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/posts\/76\/revisions"}],"predecessor-version":[{"id":1554,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/posts\/76\/revisions\/1554"}],"wp:attachment":[{"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/media?parent=76"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/categories?post=76"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/tags?post=76"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}