Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


Simplyfying code to make it run quicker

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Lua Scripting
View previous topic :: View next topic  
Author Message
Ludwig
Advanced Cheater
Reputation: 0

Joined: 10 Jan 2016
Posts: 68

PostPosted: Thu Sep 08, 2016 12:39 am    Post subject: Simplyfying code to make it run quicker Reply with quote

Good day people

im working on a script to de-obfuscate swf files...i realize lua might not be the best tool for the job, but its all that im familiar with atm...and im not a oder...

here is one of the sections of code thats taking a lot of time...and if possible i would like to simplyfy to make it run quicker
any help is apreciated...
Code:

cmp_swap1=''
cmp_swap2=''
for q=1,cmp_amt-1 do--compare amount..roughly 22 000 words of variables etc
--im running through a rabcsm decompile of the file first, which i read into class, and line 2d array
   if cmpare[q]~= '' then
      cmp_swap1='"'..cmpare[q]..'"'
      cmp_swap2='"'..cmpare[q]..'_"'
      for r=1, f_b__rab_amnt do--number of classes
         
         for rr=1, #file_array_b_rab[r] do--number of lines / class..total in all 1 700 000 lines


            file_array_b_rab[r][rr] = string.gsub(file_array_b_rab[r][rr], cmp_swap1, cmp_swap2)
            
         end
      end
   end
end
cmp_swap1=''
cmp_swap2=''
--showMessage("Array compare 1 done")

-- now i do the same with the AS3//jpex decompiled version of the file ...,roughly 900k lines of code to check
for q=1,cmp_amt-1 do
   if cmpare[q]~= '' then
      cmp_swap1=cmpare[q]
      cmp_swap2=cmpare[q]..'_'
      for r=1, f_b__jpex_amnt do
         for rr=1, #file_array_b_jpex[r] do

            x, y = string.find(file_array_b_jpex[r][rr],cmpare[q] )
            if x~=nil and y ~= nil then
               nxt_char=string.sub(file_array_b_jpex[r][rr],y+1, y+1)
               --print(nxt_char)
               if tonumber(nxt_char)==nil then--checking if the nxt character of the string ive found is indeed the correct one...some can be line var_1...where in the line its var_11, so making sure its nt a number


                  file_array_b_jpex[r][rr] = string.gsub(file_array_b_jpex[r][rr], cmp_swap1, cmp_swap2)
                  
               end
            end
         end
      end
   end
end



this specific part of code takes about 7 hours to complete in the files im doing
Back to top
View user's profile Send private message
panraven
Grandmaster Cheater
Reputation: 55

Joined: 01 Oct 2008
Posts: 942

PostPosted: Thu Sep 08, 2016 8:25 am    Post subject: Reply with quote

Break down the work load with multiple createNativeThread call may help,
but I've problem which cause access violation, so no working example, sorry.

The idea is to turn :
Code:

for q=1,cmp_amt-1 do
  <<do on list of 1...cmp_amt-1>>
end

to
Code:

local m = math.floor(cmp_amt/4) -- divid to four part
for i=1,cmp_amt-1,m do
  createNativeThread(function(th, from, to)
    for k = from,to do
      <<do on list of from...to>>
    end
  end, i --[[from]], math.min(i+m-1, cmp_amt-1)--[[to]])
end


if your pc have four core/thread, the 4 part should work on parallel.

bye~


ADDED:
make a function that use createNativeThread.

I'm confused on how to join multiple thread within a function using the Thread Class property, so I used an alternative method.

A for loop can be convert like :
Code:

for i=from,to do
  <<block>
end

to
Code:

for i=from,to do
  func(i)  -- convert from <<block>>
end

then to
Code:

function Each(first,last,f) -- define 'Each' function
  for i=first,last do
    f(i)
  end
end

Each(from,to,func) -- execute using Each function


The attached function
Code:

function asyncPartitionEach(from,to,npart,f,...)

is similar, but divide the loop into different Thread for execution, hopefully they will execute parallel on multi-thread environment to cut the total running time.

npart is a number from 1 to 7, which tell how many part to divide.

In a sample run of the test function on my i7 4 core/8 thread,
Code:

prepare
start, sampled 1000 sequencial total: 5.996 sec
1 part,    5.9930  -  expected :    5.9960
2 part,    3.1040  -  expected :    2.9980
3 part,    2.1410  -  expected :    1.9987
4 part,    1.8760  -  expected :    1.4990
5 part,    1.6170  -  expected :    1.1992
6 part,    1.5370  -  expected :    0.9993
7 part,    1.4920  -  expected :    0.8566
done

it can be seen that after 4 part, the speed up is diminishing. It should depend on the current os resource.

Note that, on general multi-threading computation, synchronization must be considered on updating same variable from different thread.

----

As to simplfying the code, I tried rearrange some test code, for example,
from
Code:

for q=1,cmp_amt-1...
  for r=...
    for rr=...
      ....gsub(some string, swap1, swap2)

to
Code:

for r=...
  for rr=...
    ....gsub(some string, Q_swap1[1], Q_swap2[1]):gsub( Q_swap1[2], Q_swap2[2]):...:gsub( Q_swap1[cmp_amt-1], Q_swap2[cmp_amt-1])

where the last part is made to a single function outside the r/rr loop, hopefully save some looping overhead. But the result on running time really have no different.

I think as long as each q,r,rr has to be loop through, the total raw cpu execution time cannot be cut down significantly.

bye~

_________________
- Retarded.
Back to top
View user's profile Send private message
Ludwig
Advanced Cheater
Reputation: 0

Joined: 10 Jan 2016
Posts: 68

PostPosted: Fri Sep 09, 2016 12:37 am    Post subject: Reply with quote

damn..thnx a lot bro...ill give it some thought and try to figure this out...its a lot to take in...Smile
hope i dont get lost on this
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Lua Scripting All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot 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 can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites