Why I need to use C for screen graphics
- Posted by Andrew Katz <Akatz712 at gmail.com> Apr 28, 2007
- 516 views
I would love to have my application be 100% Euphoria, since it claims to be such a fast language. However, I have to do some work in C because Euphoria is too slow. Euphoria is okay most of the time. But here is an example where compiled C is 20 times faster. I wrote a test case to prove it.
-- code generated by Win32Lib IDE v1.0.0 Build Apr-26-2007 constant TheProgramType="exw" include Win32Lib.ew without warning -------------------------------------------------------------------------------- -- Window Window1 constant Window1 = createEx( Window, "Test to compare Euphoria and C graphics", 0, Default, Default, 800, 600, 0, 0 ) constant DRAWIN = createEx( Window, "", Window1, 152, 0, 510, 510, {WS_CHILD, WS_CLIPSIBLINGS, WS_DLGFRAME}, {WS_EX_CLIENTEDGE, WS_EX_STATICEDGE} ) openWindow(DRAWIN, Normal) moveZOrder( DRAWIN, HWND_TOP) setWindowBackColor( DRAWIN,16777215 ) constant RUNE = createEx( PushButton, "RUN Euphoria", Window1, 28, 28, 88, 28, 0, 0 ) constant RUNC = createEx( PushButton, "Run C", Window1, 28, 68, 88, 28, 0, 0 ) --------------------------------------------------------- -------------------------------------------------------------------------------- include dll.e -- this is for calling draw_magic in C include machine.e -- this is for the container to pass to C a structure constant backcolor = #FFFFFF -- white atom draw_dll integer draw_C -- C routine handle for Euphoria draw_dll = open_dll ("DRAWtest.dll") draw_C = define_c_proc (draw_dll, "_draw_test@4", {C_UINT}) -- in C this would be: VOID draw_test(HDC hDC) -- assume device context is a UINT and draw_test has the form Watcom created in the DLL. if draw_C = -1 then abortErr( "Draw function not found" ) end if -------------------------------------------------------------------------------- procedure RUNE_onClick (integer self, integer event, sequence params)--params is () -- run Euphoria drawing integer x,y atom t0 integer MsgBox t0 = time() setWindowBackColor( DRAWIN, backcolor ) setPenMode( DRAWIN, R2_COPYPEN ) setPenColor( DRAWIN, 0 ) set_rand(1500) for i=1 to 100000 do x = rand(500) y = rand(500) drawLine ( DRAWIN, x, y, x, y+2 ) end for MsgBox = message_box(sprintf("Took %f time",{time()-t0}),"Test Euphoria", #0) end procedure setHandler( RUNE, w32HClick, routine_id("RUNE_onClick")) -------------------------------------------------------------------------------- procedure RUNC_onClick (integer self, integer event, sequence params)--params is () -- Run the C test atom hDC atom t0 integer MsgBox t0 = time() setWindowBackColor( DRAWIN, backcolor ) hDC = getDC (DRAWIN) c_proc (draw_C, {hDC}) releaseDC (hDC) MsgBox = message_box(sprintf("Took %f time",{time()-t0}),"Test C", #0) end procedure setHandler( RUNC, w32HClick, routine_id("RUNC_onClick")) WinMain( Window1,Normal )
C code: (I wonder how it is going to look inside of eucode tags)
#define STRICT #include <math.h> #include <windows.h> #include <stdlib.h> VOID __stdcall __export draw_test(HDC hDC) { int i,x,y; HPEN hcolorpen; hcolorpen = CreatePen(PS_SOLID,0,0); SetROP2(hDC, R2_COPYPEN); // drawing mode SelectObject(hDC,hcolorpen); srand(1500); for (i=0; i<100000; i++) { x = rand()%500; y = rand()%500; MoveToEx( hDC, x, y, NULL ); LineTo( hDC, x, y+2 ); } DeleteObject(hcolorpen); }
Andy Katz B.S. Computer Science, 1978 Rensselaer Polytechnic Institute (RPI)