kollo/X11Basic
5
16
Fork
You've already forked X11Basic
1

My benchmark is slow for big calculations. #10

Open
opened 2022年08月17日 09:42:03 +02:00 by RUBarzan · 5 comments

I was trying to solve this problem, with X11Basic my code ran in 33s which is too slow. I tried both of these methods:

xbasic solution.bas
xbc solution.bas && ./b.out

Then I switched to FreeBasic, my code was blazing fast! (ran in 0.1s)

I had the same experience with GNU Octave, my code was so slow; then I realised it is because in Octave, matrices are used to store data and it is generally slow with for loops.

So is this a bug? Or is it intended like Octave?

I was trying to solve [this problem](https://projecteuler.net/problem=10), with X11Basic [my code](https://www.ideone.com/D27kRi) ran in **33s** which is too slow. I tried both of these methods: ``` xbasic solution.bas xbc solution.bas && ./b.out ``` Then I switched to FreeBasic, [my code](https://www.ideone.com/YeQqnF) was blazing fast! (ran in **0.1s**) I had the same experience with GNU Octave, my code was so slow; then I realised it is because in Octave, matrices are used to store data and it is generally slow with ```for``` loops. So is this a bug? Or is it intended like Octave?
Owner
Copy link

I am not sure: It looks like that you have chousen the slowest number format and the slowest compilation option.

But why are you using big numbers here? The biggest number is supposed to be 2 Million. So the sums should still fit into 64bit floating point numbers.

I would use sum instead of sum&. Big number calculation is slow.

I am not sure: It looks like that you have chousen the slowest number format and the slowest compilation option. But why are you using big numbers here? The biggest number is supposed to be 2 Million. So the sums should still fit into 64bit floating point numbers. I would use sum instead of sum&. Big number calculation is *slow*.
Owner
Copy link
' Name: Special Pythagorean triplet
' Created: 1401年5月13日
' Language: X11Basic
 
t=TIMER
MAXN=2000000
DIM IsNat(MAXN+1)
IsNat(1)=1
sum=0
for i=2 to MAXN 
 if IsNat(i)=0
 sum=sum+i
 j=2*i
 while j<MAXN
 IsNat(j) = 1
 j=j+i
 wend
 endif
next i
PRINT sum
PRINT TIMER-t;" Seconds."
QUIT
ybasic sumprimes.bas

gives:

142915828922
1.957891941071 Seconds.
``` ' Name: Special Pythagorean triplet ' Created: 1401年5月13日 ' Language: X11Basic t=TIMER MAXN=2000000 DIM IsNat(MAXN+1) IsNat(1)=1 sum=0 for i=2 to MAXN if IsNat(i)=0 sum=sum+i j=2*i while j<MAXN IsNat(j) = 1 j=j+i wend endif next i PRINT sum PRINT TIMER-t;" Seconds." QUIT ``` <pre> ybasic sumprimes.bas </pre> gives: <pre> 142915828922 1.957891941071 Seconds. </pre>
Owner
Copy link

Even optimized a bit:

' Name: Special Pythagorean triplet
' Language: X11Basic
 
t=TIMER
MAXN=2000000
DIM IsNat(MAXN+1)
IsNat(1)=1
sum=0
FOR i=2 TO maxn
 IF IsNat(i)=0
 sum=sum+i
 j=2*i
 WHILE j<maxn
 IsNat(j)=1
 j=j+i
 WEND
 ENDIF
NEXT i
PRINT "The sum of all primes below ";maxn;" is: ";sum
PRINT TIMER-t;" Seconds."
QUIT
ybasic sumprimes.bas

gives:

The sum of all primes below 2000000 is: 142915828922
1.751878023148 Seconds.

and

xbc -virtualm sumprimes.bas
./b.out

gives:

The sum of all primes below 2000000 is: 142915828922
0.7996470928192 Seconds.

(this is the fastest, I could get).

However, If you want to sum up to higher Numbers, then you would eventually need to use the big numbers. My guess is that this would be needed for MAXN>17000000 .

Even optimized a bit: ``` ' Name: Special Pythagorean triplet ' Language: X11Basic t=TIMER MAXN=2000000 DIM IsNat(MAXN+1) IsNat(1)=1 sum=0 FOR i=2 TO maxn IF IsNat(i)=0 sum=sum+i j=2*i WHILE j<maxn IsNat(j)=1 j=j+i WEND ENDIF NEXT i PRINT "The sum of all primes below ";maxn;" is: ";sum PRINT TIMER-t;" Seconds." QUIT ``` <pre> ybasic sumprimes.bas </pre> gives: <pre> The sum of all primes below 2000000 is: 142915828922 1.751878023148 Seconds. </pre> and <pre> xbc -virtualm sumprimes.bas ./b.out </pre> gives: <pre> The sum of all primes below 2000000 is: 142915828922 0.7996470928192 Seconds. </pre> (this is the fastest, I could get). However, If you want to sum up to higher Numbers, then you would eventually need to use the big numbers. My guess is that this would be needed for MAXN>17000000 .
kollo changed title from (削除) Why is X11Basic so slow for big calculations? (削除ここまで) to My benchmark is slow for big calculations. 2022年08月17日 13:11:39 +02:00

I tried the steps you told me, Changing sum& to sum didn't make much difference (There are not many primes before 2M)

I found that the problem is with -virtualm option.

xbc -virtualm kollo.bas && ./b.out
The sum of all primes below 2000000 is: 142915828922
1.061789035797 Seconds
xbc kollo.bas && ./b.out
The sum of all primes below 2000000 is: 142915828922
33.99855804443 Seconds

(Look how close it is to 34s XD)

I'm surprised! What does -virtualm do? I think you should explain it in manual.
Overall, FreeBasic gives 0.1s and X11Basic gives 1s. I think it is decent considering X11 has more features.
Thank you a lot :)

I tried the steps you told me, Changing sum& to sum didn't make much difference (There are not many primes before 2M) I found that the problem is with ``` -virtualm ``` option. ``` xbc -virtualm kollo.bas && ./b.out The sum of all primes below 2000000 is: 142915828922 1.061789035797 Seconds ``` ``` xbc kollo.bas && ./b.out The sum of all primes below 2000000 is: 142915828922 33.99855804443 Seconds ``` (Look how close it is to 34s XD) I'm surprised! What does -virtualm do? I think you should explain it in manual. Overall, FreeBasic gives 0.1s and X11Basic gives 1s. I think it is decent considering X11 has more features. Thank you a lot :)
Owner
Copy link

The option "-virtualm" is explained here in the man-page:

https://x11-basic.codeberg.page/man-pages/xbc.1.html

The option "-virtualm" is explained here in the man-page: https://x11-basic.codeberg.page/man-pages/xbc.1.html
Sign in to join this conversation.
No Branch/Tag specified
master
unstable
crypto
1.28-65
1.28
1.27-63
1.27-62
1.27-61
1.27-60
1.27-59
1.27-58
1.27-57
1.27
1.26-57
1.26-56
1.26-55
1.26-54
1.26-53
1.26-52
1.26-51
1.26
1.25-50
1.25-49
1.25-48
1.25-47
1.25-46
1.25-45
1.25-44
1.25
1.24-42
1.24-41
1.24-40
1.24-39
1.24-37
1.24-36
1.24-35
1.24-34
1.24-33
1.23
1.24
1.18
1.15
1.01
1.00
Milestone
Clear milestone
No items
No milestone
Projects
Clear projects
No items
No project
Assignees
Clear assignees
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
kollo/X11Basic#10
Reference in a new issue
kollo/X11Basic
No description provided.
Delete branch "%!s()"

Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?