Author Topic: Classes, Unions and Arrays Help Please  (Read 20509 times)

0 Members and 1 Guest are viewing this topic.

kryton9

  • Guest
Re: Classes, Unions and Arrays Help Please
« Reply #45 on: June 17, 2012, 04:35:57 PM »
I never knew that there was a difference between return and method or function as in PowerBasic. That is very useful to know for sure.

About speed, I got good news. As I wrote before, I do my main programming on my netbook. Long battery life, built in accelerated graphics, but not powerful. Definitely not a gaming computer. So I thought, well the sqlite3 optimization's I put in did work but not fast enough on the netbook, let me try it on my gaming notebook. I am happy to report it did it all in under 11 seconds. For loading a game level of decent quality, that is very acceptable speeds.  That is around 330,000 polygons. In many of my higher end games I wait from 30 to 45 seconds for a level to load, but those are made by a team of 30 to 50 people. Even making game level over 300,000 polys is a lot of work for a single developer. So this is all very promising performance results!!!!

Let me update the code to the latest class version that you came up with so that we work with current code to see what we can break next :)

kryton9

  • Guest
Re: Classes, Unions and Arrays Help Please
« Reply #46 on: June 17, 2012, 06:59:05 PM »
I came up with this new proposal to take oxygen to the next level. No rush, but it would be nice. The attached file is not to run, just to look at in SciTe.

Charles Pegge

  • Guest
Re: Classes, Unions and Arrays Help Please
« Reply #47 on: June 17, 2012, 09:38:23 PM »

Hi Kent,

We can create a range of generic classes using macros, and substantially improve the presentation of the code.

Still need GetOperand though.

Code: OxygenBasic
  1.  
  2.  
  3. '==============================
  4. macro OperatorClass(name,tbody)
  5. '==============================
  6.  
  7.   type type##name tbody
  8.  
  9.   class name
  10.  
  11.   static type##name accum[32]
  12.   static type##name *a, *b
  13.  
  14.   static sys accumIndex, accumPopped
  15.  
  16.   has type##name
  17.  
  18.   macro GetOperand  
  19.     if accumPopped then  
  20.       @b = @a + sizeof accum
  21.       accumPopped = 0  
  22.     else  
  23.       @b = @this
  24.     end if  
  25.   end macro  
  26.  
  27.   method "load"()
  28.     @a = @accum + accumIndex
  29.     GetOperand  
  30.     copy @a,@b,sizeof accum
  31.   end method  
  32.  
  33.   method "="()
  34.     GetOperand
  35.     copy @b,@a,sizeof accum
  36.   end method  
  37.  
  38.   method "push"()  
  39.     accumIndex += sizeof accum  
  40.   end method  
  41.  
  42.   method "pop"()  
  43.     accumPopped = 1
  44.     accumIndex -= sizeof accum  
  45.     @a = @accum + accumIndex
  46.   end method
  47.  
  48. end macro
  49.  
  50.  
  51.  
  52. OperatorClass (Vector,{double x,y,z})
  53. '====================================
  54.  
  55.  
  56.   method constructor(double ax=0,ay=0,az=0)
  57.     this<=ax,ay,az
  58.   end method
  59.  
  60.   method destructor()
  61.   end method
  62.  
  63.  
  64.   method "+"()
  65.     getoperand  
  66.     a.x += b.x  
  67.     a.y += b.y  
  68.     a.z += b.z  
  69.   end method  
  70.  
  71.   method "-"()
  72.     getoperand  
  73.     a.x -= b.x  
  74.     a.y -= b.y  
  75.     a.z -= b.z  
  76.   end method  
  77.  
  78.   method "*"()
  79.     getoperand  
  80.     a.x *= b.x  
  81.     a.y *= b.y  
  82.     a.z *= b.z  
  83.   end method  
  84.  
  85.   method "/"()
  86.     getoperand  
  87.     a.x /= b.x  
  88.     a.y /= b.y  
  89.     a.z /= b.z  
  90.   end method  
  91.  
  92. '...
  93.  
  94. end class
  95.  
  96. '#recordof Vector
  97.  
  98. new vector v1 (1,2,3)
  99. new vector v2 (10,20,30)
  100. new vector v3
  101. v3=v1+v2*v2
  102.  
  103. print v3.x "," v3.y "," v3.z 'result 101,402,903
  104.  

Charles

kryton9

  • Guest
Re: Classes, Unions and Arrays Help Please
« Reply #48 on: June 17, 2012, 10:23:56 PM »
That is neat Charles. I can live happily with that syntax! Very clever as always!!!

The code does not run. I think when pasted in, it put nasties in. I noticed it did that with mine the other day. Can you attach the code, thanks.

Now one thing however. In games we need to use threads. I am not up to speed on using threads other than the little tests. I remember you said we couldn't thread something we were doing, so an area that would need work is making us be able to use our classes in threads. No hurry, but something that eventually needs to be addressed.

Here is a bottleneck my speed tests from the last few days lead too. I optimized Sqlite3, so it is running very fast, even on my netbook. It is when doing string += 's.
I then went to a string array and that is very fast, but then putting that array into 1 string bottlenecks again. When you do an insert of many records, being in one string file makes it dramatically faster.
Anyways, this is older, but very experimental files so keep them separate as I do in its own folder.

So either we need a new faster string += or be able to pass a string array. I couldn't figure it out and took out all my attempt code to make it easier to see what is going on.

Here is my latest speedtest from my weak netbook.
« Last Edit: June 17, 2012, 10:32:22 PM by kryton9 »

Charles Pegge

  • Guest
Re: Classes, Unions and Arrays Help Please
« Reply #49 on: June 17, 2012, 10:48:56 PM »

Thanks Kent,

Here is the program. Do you have the new dll? (above). it has to go in the Oxygen base directory.

kryton9

  • Guest
Re: Classes, Unions and Arrays Help Please
« Reply #50 on: June 17, 2012, 11:01:35 PM »
Thanks, that worked fine. Will look at in detail now.

kryton9

  • Guest
Re: Classes, Unions and Arrays Help Please
« Reply #51 on: June 17, 2012, 11:09:27 PM »
How come you are not deleting the new vectors that were created Charles?

kryton9

  • Guest
Problem with Macro Way
« Reply #52 on: June 17, 2012, 11:22:55 PM »
Charles, I found some problems with the macro way.

Mainly, lost private, protected and public for vars and methods.
Here is my code to test. It won't compile because of the public.

kryton9

  • Guest
speed faster, but can be with one more tweak
« Reply #53 on: June 17, 2012, 11:56:46 PM »
This is a continuation of my speed tests from this post: http://www.oxygenbasic.org/forum/index.php?topic=461.msg3632#msg3632

Both of these have optimized sqlite3 setups. The post above generates 1 million vec3's. But since it is slow only puts into the sqlite3 database 20,000.
It took around 89 seconds on my netbook as the screenshot shows.

Now here is a version where I bypass the choke with strings and put one at a time into sqlite3 a million. That is it generates and puts 1 million into the database in about
47 seconds.

Once we get a single string sent with all inserts in one string, it should be as fast as it can be, even on my netbook!

Charles Pegge

  • Guest
Re: Classes, Unions and Arrays Help Please
« Reply #54 on: June 18, 2012, 01:30:26 AM »
The thread problem:

Any class or procedure that relies on static variables is likely to run into state conflicts when more than one thread tries to use them.

The solution is really quite simple: Parcel all the classes and procedures concerned, into a macro, then invoke the macro a the start of each thread. Then there will be no possibility of conflict.


macro OperatorClassCluster

OperatorClass Vector2f, {single x,y}
...
end class

OperatorClass Vector3d, {double x,y,z}
...
end class

...

end macro



This generates extra code but the alternatives are too awful to contemplate  ;)
« Last Edit: June 18, 2012, 01:52:35 AM by Charles Pegge »

Charles Pegge

  • Guest
Re: Classes, Unions and Arrays Help Please
« Reply #55 on: June 18, 2012, 01:44:23 AM »
Using OperatorClass

You can put restrictors inside the macro

macro OperatorClass(name,tbody)

  type type##name tbody

  class name

  protected

  static type##name accum[32]
  static type##name *a, *b

  static sys accumIndex, accumPopped

  has type##name

  macro GetOperand  
    if accumPopped then  
      @b = @a + sizeof accum
      accumPopped = 0  
    else  
      @b = @this
    end if  
  end macro  


  public


« Last Edit: June 18, 2012, 01:54:00 AM by Charles Pegge »

Charles Pegge

  • Guest
Re: Classes, Unions and Arrays Help Please
« Reply #56 on: June 18, 2012, 02:48:54 AM »
Kent,

My speed test results:

Overall: 16.6 seconds

This breaks down approximately as follows

Randomiser calls: 0.15

Conversion of doubles to strings: 2.38

Concatenation of strings: 2.15


which leaves about 12 seconds for sqlite processing

my test rig:

                   tSub.Start
                        a.Begin
string s
string d1="123456789",d2="123456789",d3="123456789"
'double d1,d2,d3
                            for x = 1 to 1000000
'd1=RandD(rx,ry,rz)
'd2=RandD(rx,ry,rz)
'd3=RandD(rx,ry,rz)
                               s= "insert into Enterprise values ( "RandD( rx, ry, rz )","RandD( rx, ry, rz )","RandD( rx, ry, rz )" );" crlf
                                'a.put( "insert into Enterprise values ( "RandD( rx, ry, rz )","RandD( rx, ry, rz )","RandD( rx, ry, rz )" );" crlf )
                                'a.put( "insert into Enterprise values ( "d1","d2","d3" );" crlf )
                            next
                        a.End
                    tSub.Stop
« Last Edit: June 18, 2012, 03:01:46 AM by Charles Pegge »

kryton9

  • Guest
Re: Classes, Unions and Arrays Help Please
« Reply #57 on: June 18, 2012, 11:41:43 AM »
I guess the speed is fine if you are on a computer that can play high quality games.

But, if I could put the string array into a string with some pointer magic and pass that one string, even on a weak system we would get great speed, I am sure. So if you know any tricks let me know!

I will test your macro updated code later tonight when I have time. Thanks.

kryton9

  • Guest
Running single string into sqlite, saved 10 seconds
« Reply #58 on: June 18, 2012, 06:58:42 PM »
I dumped the data from the sqlite database of 1 million random double Vec3's into a text file.
I was then able to test what a difference it would make. It was 25% faster on my slow netbook.
Before single inserts, around 47 seconds.
Now around 36 to 37 seconds.

With the sqlite optimizations writing to hard disk or doing in memory are almost the same amount of time.
Which is impressive.

Charles Pegge

  • Guest
Re: Classes, Unions and Arrays Help Please
« Reply #59 on: June 18, 2012, 07:44:06 PM »
Hi Kent,

The benchmarks I ran suggest the 25% time reduction is due to eliminating millions of float-to-string conversions and  string concatenations.