 |
Bay Six Software Beyond the Basics
|
View previous topic :: View next topic |
Author |
Message |
Brent Site Admin
Joined: 01 Jul 2005 Posts: 793
|
Posted: Jun 18th, 2007, 1:56am Post subject: STRUCT slowness |
|
|
[See here for the genesis of this topic.]
My VFScript language uses some pretty big STRUCTs to construct what are essentially "enumerations" (ENUMs in many languages), a list of named constants which are assigned unique values, respectively, without much regard to what those values are. My main reason for using them was to make the code a bit easier to read and modify.
I assumed that accessing STRUCT elements was as fast (if not faster than) accessing a normal variable. Boy, was I wrong. Try this Code: | t = time$("ms")
for i = 1 to 100000
a = int(rnd(1) * 100)
if a < 100 then b = a
next
print time$("ms") - t
struct s, a as long, b as long
t = time$("ms")
for i = 1 to 100000
s.a.struct = int(rnd(1) * 100)
if s.a.struct < 100 then s.b.struct = s.a.struct
next
print time$("ms") - t
end |
The STRUCT accesses are consistantly 3 times slower than normal variables. I'd be interested to learn a way to keep the same or similar level of flexibility without the performance hit. _________________ Brent |
|
Back to top |
|
 |
Beginnerone Guest
|
Posted: Jun 18th, 2007, 5:06am Post subject: Re: STRUCT slowness |
|
|
This would only work if the struct had to be manipulated many times per loop.
Code: | 'initialize everything
struct s, a as long, b as long, c as long
a= 5
b= 8
s.a.struct = a
s.b.struct = b
'Start
'Regular INT loop
t = time$("ms")
for i = 1 to 10000
c = a+b
c = a*b
c = a-b
b = a+c
b = a*c
b = a-c
a = c+b
a = c*b
a = c-b
next
print "Regular Integer time : "; time$("ms") - t
a= 5
b= 8
s.a.struct = a
s.b.struct = b
'Regular STRUCT loop
t = time$("ms")
for i = 1 to 10000
s.c.struct = s.a.struct+s.b.struct
s.c.struct = s.a.struct*s.b.struct
s.c.struct = s.a.struct-s.b.struct
s.b.struct = s.a.struct+s.c.struct
s.b.struct = s.a.struct*s.c.struct
s.b.struct = s.a.struct-s.c.struct
s.a.struct = s.b.struct+s.c.struct
s.a.struct = s.b.struct*s.c.struct
s.a.struct = s.b.struct-s.c.struct
next
print "Regular Struct time : "; time$("ms") - t
a= 5
b= 8
s.a.struct = a
s.b.struct = b
'Special Struct loop w/ func.
t = time$("ms")
for i = 1 to 10000
a = s.a.struct
b = s.b.struct
s.a.struct = StructChange(c,b)
s.b.struct = StructChange(a,c)
s.c.struct = StructChange(a,b)
next
print "Special shortcut Struct Time w/ func. : "; time$("ms") - t
'Special Struct loop without func.
a= 5
b= 8
s.a.struct = a
s.b.struct = b
t = time$("ms")
for i = 1 to 10000
a = s.a.struct
b = s.b.struct
c = a+b
c = a*b
c = a-b
b = a+c
b = a*c
b = a-c
a = c+b
a = c*b
a = c-b
s.a.struct = a
s.b.struct = b
s.c.struct = c
next
print "Special shortcut Struct Time without func. : "; time$("ms") - t
end
Function StructChange(Num1, Num2)
d = Num1+Num2
d = Num1*Num2
d = Num1-Num2
StructChange = Num0
End function |
Okay, the first part is a normal int manipulation loop, the second is a normal struct manipulation loop. The third part manipulates structs though functions. The last one is the fastest of struct manipulations, however you risk errors with clashing variables. (Ex. if you used "b" as a variable somewhere else in the application.)
Sorry Brent, but I don't think this really applies to the problem with your VFScript, because you only access the struct like 4 times per loop. If I find any solution to your example, I'll post that too.  |
|
Back to top |
|
 |
RichardRussell Full Member
Joined: 28 Jan 2012 Posts: 57 Location: Downham Market, UK
|
Posted: Feb 4th, 2012, 11:09pm Post subject: Re: STRUCT slowness |
|
|
Brent wrote: | The STRUCT accesses are consistantly 3 times slower than normal variables. |
Another LB 4.04 vs LBB 1.70 comparison:
Code: | Ordinary variable: LB 1.80 LBB 0.69
Structure member: LB 4.53 LBB 1.31 |
So in LB4 the structure member is about 2.5 times slower than a regular variable, and in LBB about twice as slow. In a compiled language there would be no reason to expect a structure member to be any slower, but with an interpreter there's bound to be an overhead.
Richard. |
|
Back to top |
|
 |
|
|
|
|
|
You can post new topics in this forum You can reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You cannot download files in this forum
|
|