I am a Beginner, could you help please?.
-
Hi guys this is simple but I'm a novice trying to cache the getMyresult function but I'm struggling to find the code to do so, could you help please? :|. I don't want the answer just some of the methods and syntax.
Sub Main
console.writeline(getmyresult(2)) 'takes a while'
console.writeline(getmyresult(3)) 'takes a while'
console.writeline(getmyresult(2)) 'Should be instant'
console.writeline(getMyresult(3)) 'Should be instant'
End Subfunction getMyresult(X as interger) as integer
dim Y as integer=LongCompute(X)
return Y
end functionfunction LongCompute(X as integer) as integer
system.threading.thread.sleep(1000)
return x^2
end function -
Hi guys this is simple but I'm a novice trying to cache the getMyresult function but I'm struggling to find the code to do so, could you help please? :|. I don't want the answer just some of the methods and syntax.
Sub Main
console.writeline(getmyresult(2)) 'takes a while'
console.writeline(getmyresult(3)) 'takes a while'
console.writeline(getmyresult(2)) 'Should be instant'
console.writeline(getMyresult(3)) 'Should be instant'
End Subfunction getMyresult(X as interger) as integer
dim Y as integer=LongCompute(X)
return Y
end functionfunction LongCompute(X as integer) as integer
system.threading.thread.sleep(1000)
return x^2
end functionGetMyResult has to maintain it's own cache of result values. Create a collection to store Key/Value pairs, usually a Dictionary(Of Integer, Integer). The Key value will be the integer you pass in and the result will be the value returned by LongCompute. Basically, when GetMyResult is called, it uses the value passed in to search the Dictionary for that value. If it's found, it can return the value associated with that key. If it's not found, then you call LongCompute with the passed in value, get the result, and add the new key/value pair to the dictionary. Lastly, return the result back to the caller.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak -
Hi guys this is simple but I'm a novice trying to cache the getMyresult function but I'm struggling to find the code to do so, could you help please? :|. I don't want the answer just some of the methods and syntax.
Sub Main
console.writeline(getmyresult(2)) 'takes a while'
console.writeline(getmyresult(3)) 'takes a while'
console.writeline(getmyresult(2)) 'Should be instant'
console.writeline(getMyresult(3)) 'Should be instant'
End Subfunction getMyresult(X as interger) as integer
dim Y as integer=LongCompute(X)
return Y
end functionfunction LongCompute(X as integer) as integer
system.threading.thread.sleep(1000)
return x^2
end functionWhy are you sleeping? When you put in a "sleep" the the application goes to sleep. You are blocking the thread from running. There are some instances where you need to use 0, but that is rare. And I would write this ... Function GetResult (ByVal num as Integer) as Integer return num^2 End Function Getmyresult is not needed at all. Sleep is not needed. Also, Public Main and Private Function. When you say "cache", I am not sure if you are using the term, but you can cache ... http://stackoverflow.com/questions/533861/writing-a-cache-function-in-vb-net[^] The cache class ... http://msdn.microsoft.com/en-us/library/system.web.caching.cache.aspx[^]
-
GetMyResult has to maintain it's own cache of result values. Create a collection to store Key/Value pairs, usually a Dictionary(Of Integer, Integer). The Key value will be the integer you pass in and the result will be the value returned by LongCompute. Basically, when GetMyResult is called, it uses the value passed in to search the Dictionary for that value. If it's found, it can return the value associated with that key. If it's not found, then you call LongCompute with the passed in value, get the result, and add the new key/value pair to the dictionary. Lastly, return the result back to the caller.
A guide to posting questions on CodeProject[^]
Dave KreskowiakJust curious Dave and Poster, but what does this mean in context - "cache"? I guess the simplicity of the code has me puzzled - two-stepping for a single computation. I can't see beyond that. I use a dictionary and actually truly cache with the class. Purpose?
-
Why are you sleeping? When you put in a "sleep" the the application goes to sleep. You are blocking the thread from running. There are some instances where you need to use 0, but that is rare. And I would write this ... Function GetResult (ByVal num as Integer) as Integer return num^2 End Function Getmyresult is not needed at all. Sleep is not needed. Also, Public Main and Private Function. When you say "cache", I am not sure if you are using the term, but you can cache ... http://stackoverflow.com/questions/533861/writing-a-cache-function-in-vb-net[^] The cache class ... http://msdn.microsoft.com/en-us/library/system.web.caching.cache.aspx[^]
Because his code is an example!
A guide to posting questions on CodeProject[^]
Dave Kreskowiak -
Just curious Dave and Poster, but what does this mean in context - "cache"? I guess the simplicity of the code has me puzzled - two-stepping for a single computation. I can't see beyond that. I use a dictionary and actually truly cache with the class. Purpose?
To hold a table of values that have already been computed so you don't have to run through a potentially time-consuming calculation again to return the same value for the given input.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak -
To hold a table of values that have already been computed so you don't have to run through a potentially time-consuming calculation again to return the same value for the given input.
A guide to posting questions on CodeProject[^]
Dave KreskowiakExactly the same way that I do on the accounting side of my apps, but I exlicitly set out to cache. Sorry to inject! I had curiosity get the best of me as I was looking at the code and realizing that it is a textbook example has answered the question. Once again, sorry! Thanks for the replies, and let me know if you solve the threading problem. LOL ... Attempted multitasking this day. :wtf:
-
Because his code is an example!
A guide to posting questions on CodeProject[^]
Dave KreskowiakDear Dave, Many thanks for explaining it to me as well instead of just giving me the code, My teacher will be impressed haha Many Thanks 101
-
Why are you sleeping? When you put in a "sleep" the the application goes to sleep. You are blocking the thread from running. There are some instances where you need to use 0, but that is rare. And I would write this ... Function GetResult (ByVal num as Integer) as Integer return num^2 End Function Getmyresult is not needed at all. Sleep is not needed. Also, Public Main and Private Function. When you say "cache", I am not sure if you are using the term, but you can cache ... http://stackoverflow.com/questions/533861/writing-a-cache-function-in-vb-net[^] The cache class ... http://msdn.microsoft.com/en-us/library/system.web.caching.cache.aspx[^]
Prissy, Thank you very much for the code I appreaciate the help im glad Dave explained it to me as this would of been cheating :P :-O
-
Prissy, Thank you very much for the code I appreaciate the help im glad Dave explained it to me as this would of been cheating :P :-O
I don't do it any other way! Good Luck!
A guide to posting questions on CodeProject[^]
Dave Kreskowiak -
Exactly the same way that I do on the accounting side of my apps, but I exlicitly set out to cache. Sorry to inject! I had curiosity get the best of me as I was looking at the code and realizing that it is a textbook example has answered the question. Once again, sorry! Thanks for the replies, and let me know if you solve the threading problem. LOL ... Attempted multitasking this day. :wtf:
Threading problem?? What threading problem??
A guide to posting questions on CodeProject[^]
Dave Kreskowiak -
Hi guys this is simple but I'm a novice trying to cache the getMyresult function but I'm struggling to find the code to do so, could you help please? :|. I don't want the answer just some of the methods and syntax.
Sub Main
console.writeline(getmyresult(2)) 'takes a while'
console.writeline(getmyresult(3)) 'takes a while'
console.writeline(getmyresult(2)) 'Should be instant'
console.writeline(getMyresult(3)) 'Should be instant'
End Subfunction getMyresult(X as interger) as integer
dim Y as integer=LongCompute(X)
return Y
end functionfunction LongCompute(X as integer) as integer
system.threading.thread.sleep(1000)
return x^2
end functionIs it VB 6 or VB .NET? I don't know about return statement in VB 6? I'm forgot to choose Question. Oh, it's a VB .NET Discussion. I don't know. Sorry.
-
Is it VB 6 or VB .NET? I don't know about return statement in VB 6? I'm forgot to choose Question. Oh, it's a VB .NET Discussion. I don't know. Sorry.
Haha