optimization

Efficient way to hash a 2D point

你说的曾经没有我的故事 提交于 2019-12-31 03:12:08
问题 OK, so the task is this, I would be given (x, y) co-ordinates of points with both (x, y) ranging from -10^6 to 10^6 inclusive. I have to check whether a particular point e.g. (x, y) tuple was given to me or not. In simple words how do i answer the query whether a particular point(2D) is set or not. So far the best i could think of is maintaining a std::map<std::pair<int,int>, bool> and whenever a point is given I mark it 1. Although this must be running in logarithmic time and is fairly

Should I store a calculation in a variable if it will be used a lot?

烈酒焚心 提交于 2019-12-31 02:53:07
问题 If I have a function to, for example, check if list1 is a sublist of list2 , which option is better: Option 1: def isSublist1(list1,list2): "This fuction checks if list1 is a sublist of list2." for i in range(len(list2)): part = list2[i:] # part is a list with all the elements from i to the end of list2 if len(part)<len(list1): return False if list1==part[:len(list1)]: # if list1 is in the beginning of part return True return False Or option 2: def isSublist2(list1,list2): "This fuction

Sharing image and text to Facebook Messenger with UIActivityViewController failing

流过昼夜 提交于 2019-12-31 02:13:17
问题 Question What changes to the code below must be made to ensure Messenger plays nicely with UIActivityViewController and shares both the image and text, or at the very least, the image? Background I am using UIActivityViewController to share text and images from my app and send them to email, messages and other sharing apps. UIActivityViewController is great and works in a simple and standard way with most apps… but, I’m having issues with Messenger (Facebook Messenger) which doesn’t want to

Can this python code be more efficient?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-31 01:56:31
问题 I have written some code to find how many substrings of a string are anagram pairs. The function to find anagram(anagramSolution) is of complexity O(N). The substring function has complexity less than N square. But, this code here is the problem. Can it be more optimized? for i in range(T): x = raw_input() alist = get_all_substrings(x) for k, j in itertools.combinations(alist,2): if(len(k) == len(j)): if(anagramSolution(k,j)): counter +=1 counterlist.append(counter) counter = 0 The alist can

Efficiently dividing unsigned value by a power of two, rounding up - in CUDA

情到浓时终转凉″ 提交于 2019-12-31 01:46:11
问题 I was just reading: Efficiently dividing unsigned value by a power of two, rounding up and I was wondering what was the fastest way to do this in CUDA. Of course by "fast" I mean in terms of throughput (that question also addressed the case of subsequent calls depending on each other). For the lg() function mentioned in that question (base-2 logarithm of divisor), suppose we have: template <typename T> __device__ int find_first_set(T x); template <> __device__ int find_first_set<uint32_t>

Optimizing Python Dictionary Lookup Speeds by Shortening Key Size?

我怕爱的太早我们不能终老 提交于 2019-12-31 00:03:08
问题 I'm not clear on what goes on behind the scenes of a dictionary lookup. Does key size factor into the speed of lookup for that key? Current dictionary keys are between 10-20 long, alphanumeric. I need to do hundreds of lookups a minute. If I replace those with smaller key IDs of between 1 & 4 digits will I get faster lookup times? This would mean I would need to add another value in each item the dictionary is holding. Overall the dictionary will be larger. Also I'll need to change the

quickest way to dynamically retrieve image dimensions

安稳与你 提交于 2019-12-30 18:32:51
问题 One way to improve page loading is to specify image dimesions (hieght width). In PHP this can be done with getimagesize(), however I can imagine this would be quite slow to execute if you have alot of images. What is the best way to dynamically get image dimensions of many images with minimal effect on page loading. We are talking about 50+ images. 回答1: I've just tested with 55 pcs of 5+ MB images: Imagemagick's getImageGeometry took 5.3 seconds (because after each file you have to recreate

Fast float to int conversion (truncate)

…衆ロ難τιáo~ 提交于 2019-12-30 17:27:10
问题 I'm looking for a way to truncate a float into an int in a fast and portable (IEEE 754) way. The reason is because in this function 50% of the time is spent in the cast: float fm_sinf(float x) { const float a = 0.00735246819687011731341356165096815f; const float b = -0.16528911397014738207016302002888890f; const float c = 0.99969198629596757779830113868360584f; float r, x2; int k; /* bring x in range */ k = (int) (F_1_PI * x + copysignf(0.5f, x)); /* <-- 50% of time is spent in cast */ x -= k

Merging data from many files and plot them

只谈情不闲聊 提交于 2019-12-30 13:31:11
问题 I have written application that is analyzing data and writing results in CSV file. It contains three columns: id , diff and count . 1. id is the id of the cycle - in theory the greater id, the lower diff should be 2. Diff is the sum of (Estimator - RealValue)^2 for each observation in the cycle 3 count is number of observation during cycle For 15 different values of parameter K, I am generating CSV file with name: %K%.csv , where %K% is the used value. My total number of files is 15. What I

Merging data from many files and plot them

旧巷老猫 提交于 2019-12-30 13:31:10
问题 I have written application that is analyzing data and writing results in CSV file. It contains three columns: id , diff and count . 1. id is the id of the cycle - in theory the greater id, the lower diff should be 2. Diff is the sum of (Estimator - RealValue)^2 for each observation in the cycle 3 count is number of observation during cycle For 15 different values of parameter K, I am generating CSV file with name: %K%.csv , where %K% is the used value. My total number of files is 15. What I