View Full Version : thinBASIC coding challenge #1
Petr Schreiber
05-04-2007, 21:05
Hi guys,
I think some coding challenge from time to time could be nice thing, showing how thinBASIC can be used.
I would recommend basic, common tasks.
Here is my idea:
Assignment
Write a script which will count all characters in text stored in inString.
You can use thinBASIC + all standard modules ( which come with default installation ).
Please use following skeleton, but code should be general - to count occurience of all possible characters from ASCII 0 to 255 ( do not optimize just for characters in "sample" string below )
dim inString as string = repeat$(1000,"Hi there, how it goes ?"+CHR$(0))
dim t1, t2 as quad
t1 = gettickcount
' ... insert code here ...
t2 = gettickcount
' ... display results here ...
Output must look as on attached image ( sorted from most to less used ).
Winner is...
...person achieving shortest time of character count :)
End of contest
Friday 13th, April 2007 :) CHANGED
I am looking forward to your tries!,
Petr
P.S. My version is attached below but do not download it till Friday !
ErosOlmi
06-04-2007, 00:26
Hey Petr,
thanks for the idea. Nice, nice one.
Created a dedicated forum.
Eros
Sounds good, I am not good at optimizing but going the other way, slowing down :)
Might be good idea to test all final scripts on the same computer to see comparative speeds.
I hope have time to get involved, been hectic and still lots to do. Good luck!!
Petr Schreiber
06-04-2007, 07:47
Hi,
Eros, thanks a lot for new section ;)
kryton - do not give up :)
If you will need more time I can move the deadline few days more.
Comparsion on one computer would be nice, also it is the only method which should reflect results correctly.
Bye,
Petr
RobertoBianchi
06-04-2007, 08:45
Coding challenger?
I can't be out, what are rules? Can I develop a specific module?
Please (if possible) extend the expiration date, I will be in vacation until the april 10. >:(
Ciao,
Roberto.
Michael Hartlef
06-04-2007, 09:10
Good idea, but count even characters below 32?
ErosOlmi
06-04-2007, 10:07
Roberto,
rule is simple: just count every ascii char in inString and for each report number of occurences.
Petr did also a substitute loop to be able to print out chars below code 32.
Ciao
Eros
PS: vacation? ???
Who authorized them ;D
Petr Schreiber
06-04-2007, 10:20
Hi,
Mike - yes, of course :) Full old good ASCII ( tabelator as well as cr, lf are used frequently )
Roberto - as you have no authorization for vacations ... :D
Ok, moved end of contest to Firday 13th to make contest accessible for all.
But I can't accept module as contest entry, you would depress us with some assembly light speed ;D
Just thinBASIC + any of standard modules.
Bye and thanks,
Petr
ErosOlmi
06-04-2007, 13:01
OK, here it is my version.
I will entitle it "The power of pointers". See results :D
Ciao
Eros
UPDATED: added comments
RobertoBianchi
06-04-2007, 13:05
Wow I see, you are power(s basic) users ;D
Here is my version
uses "Stat"
dim inString as string = repeat$(1000,"Hi there, how it goes ?"+CHR$(0))
dim n as long = len(inString)
dim aData1(n) as byte
dim aData2(n*2) as DWORD
dim aData(256) as string
dim i as long
dim sText as string
dim s as string
dim t1, t2 as quad
t1 = gettickcount
poke$(varptr(aData1(1)),PeeK$(STRPTR(inString),n))
Stat_CopyArray(aData1, aData2)
Stat_Histogram(aData2,1)
for i=1 to 256
if aData2(i+n)>0 then
select case aData2(i)
case 0
s=" NUL"
case 32
s=" SPC"
case else
s=" "+chr$(aData2(i))
end select
aData(i)=ltrim$(str$(aData2(i + n)))+" "+s
end if
next
array sort aData, descend
for i=1 to 256
if len(aData(i)) then
sText=sText+aData(i)+$CRLF
end if
next
t2 = gettickcount
msgbox 0, "Result of text analysis ("+ltrim$(STR$(n))+" bytes)"+$CRLF+$CRLF+sText+$CR+$CR+"Operation took:"+STR$( (t2-t1)/1000, 3)+" seconds", %MB_OK or %MB_ICONINFORMATION, "Results"
I don't post result here, please take a pleasure to discovery it by yourself.
Regards,
Roberto
ErosOlmi
06-04-2007, 13:09
WOW, Roberto put in the battle field one of his creature: STAT module !
My version was 10 times faster than Petr one.
Roberto version is 4 times faster than mine.
Quite unbeatable!
Michael Hartlef
06-04-2007, 14:49
does the sorting have to be done inside the timecounting or outside?
RobertoBianchi
06-04-2007, 16:24
Hello Mike,
rules should be given before not after. ::)
However you should absolutly make the sort inside the timecounting plus two clyce for next like me and Petr and optionally run a
sleep 100 statement! ;D
Ciao,
Roberto
ErosOlmi
06-04-2007, 16:31
Mike,
sorry about Roberto comment ???
He activated pre-vacation flag ;D
To beat Roberto code the only way is:
dim t1, t2 as quad
t1 = gettickcount
'---Do nothing
t2 = gettickcount
msgbox 0, "Result of text analysis (" + STR$( (t2-t1)/1000, 3) + " seconds)", %MB_OK or %MB_ICONINFORMATION, "Results"
;D
RobertoBianchi
06-04-2007, 17:09
Ops, excused me I have overstate I wanted to say SLEEP 10 instead of SLEEP 100.
You can't belive but I found a tipical italian spaghetti code that bets my code although it executes a SLEEP 10 instruction.
Here is the power code:
dim t1, t2 as quad
dIM s AS STRING
t1 = gettickcount
s="5000 SPC"+$CRLF+"3000 e"+$CRLF+"2000 t"+$CRLF+"2000 o"+$CRLF+"2000 i"+$CRLF+"2000 h"+$CRLF+"1000 w"+$CRLF
s=s+"1000 s"+$CRLF+"1000 r"+$CRLF+"1000 g"+$CRLF+"1000 NUL"+$CRLF+"1000 H"+$CRLF+"1000 ?"+$CRLF+"1000 ,"+$CRLF
sleep 10
t2 = gettickcount
msgbox 0,"Result of text analysis (24000 bytes)"+$CRLF+$CRLF+s+$CR+$CR+"Operation took:"+STR$( (t2-t1)/1000, 3)+" seconds", %MB_OK or %MB_ICONINFORMATION, "Results"
Good Easter to all,
Roberto
ErosOlmi
06-04-2007, 17:27
Roberto,
next time you will have so nice code to post, please obfuscate it so we will not see such a mess.
Go holiday, please :D
Ciao. See you next week.
Eros
RobertoBianchi
06-04-2007, 17:34
Apart the jokes, personally I have to thanks Petr because through the Coding Challenge it has given the occasion to me to show an example of thinbasic_stat.dll module and also understand that some new functionality should be usefull if added to the module. At least this until now because the challenge continue.
Regards,
Roberto
I guess we are allowed to look at the code now. I will study them this weekend as not sure what is going on quite with the challenge. But reading Eros's post about the speed increases in each of your examples, dramatic speed improvements.
I am sure these challenges will be very entertaining and educating too, hoping for some time to look this weekend!!
Petr Schreiber
06-04-2007, 20:33
Hi,
Eros, Roberto ... what did you do to me :)
Very fast codes, as usual I forgot thinBASIC pointer magic and STAT module! :D
Thanks!,
Petr
Michael Hartlef
06-04-2007, 20:36
Petr, I think Eros and Roberto are disqualified ;D Eros looked at Roberto's code before friday the 13th and Roberto used the power of modules ;D
But I forgive them both, my mind is on vacation mode too.
So it is only between Petr and me, he he. ;) But only if Kent won't participate.
Here is my entry. Not sure how fast it is.
Michael
Michael Hartlef
06-04-2007, 20:40
Sorry Roberto, I saw you were allowed to use the stat module. Ok, I hope I have a chance then. Have a nice vacation. ;)
ErosOlmi
06-04-2007, 21:22
Mike is right.
I have to be out of the game because I didn't follow the rule to not look at the code.
Sorry Petr. I will try to make my best to be pardoned.
:-[
Michael Hartlef
06-04-2007, 22:59
Dooo, I was kidding Eros, of course you are allowed to compete further. ;)
Petr Schreiber
07-04-2007, 15:37
Hi,
no problem Eros,
purpose of this challenge is to have multiple samples of coding the same thing :)
Mike, I will test your version as soon as I'll get on thinBASIC powered PC, thanks !
Bye,
Petr
Petr Schreiber
08-04-2007, 14:18
Hi Mike,
your code is very fast,
speed is very similar to Eros attempt,
although you use your own implementation of sort !
Just one tip to make your version even faster- instead of temporary variable use,
try to enter this in sort loop:
swap sa(i,1), sa(i+1,1)
swap sa(i,2), sa(i+1,2)
Thanks,
Petr
Michael Hartlef
08-04-2007, 18:13
Thanks Petr,
I was looking for something like this, but could not find it. That is why I did it this way.
But for sure, I will try this once I'm back.
Looking at the challenge code, that is neat how you guys all came up with different approaches!!
On my computer the results were as follows, I ran the tests twice to confirm:
1. Roberto an amazing 0 seconds on my computer
2. Petr at .036
3. Amazing but true a tie between Mike and Eros at .047
Petr Schreiber
10-04-2007, 11:22
Hi kryton,
it seems your PC is the only one which likes my code :)
Here are my results:
Roberto 0.03
Eros 0.179 <- sometimes this is Mikes time
Mike 0.189 <- sometimes this is Eros time
Me :) 1.22
Bye,
Petr
ErosOlmi
10-04-2007, 15:54
Results on my machine:
Roberto 0.016
Eros 0.038
Mike 0.047
Petr 0.500
It seems a paper or help file will need to be written, for optimizing according to the CPU being used. Not really, but amazing the difference in the results.