Loops are a type of control flow structure in programming in which a series of statements may be executed repeatedly until some condition is met.
4
votes
2answers
47 views
Optimizing if-statement with for-loop
I have been staring at this code for awhile now and I am thinking there is a way to optimize it (namely the if-else statement with the for-loops). Any advice would be greatly appreciated.
...
5
votes
2answers
361 views
Can this foreach loop be improved?
Can the below foreach loop be improved? Since I use the same method twice, maybe use continue or break?
int? index = null;
int count = 0;
foreach (Break b in breaks)
{
if (index.HasValue)
{
...
4
votes
3answers
104 views
Does this loop unrolling make sense?
I am coding a Hanning filter which is a recursive relation:
y[i] = 0.25*(x[i] + 2*x[i-1] + x[i-2])
And here is my code were I attempt to unroll 3 times:
public void HanningFilter(float[] array) { ...
0
votes
1answer
53 views
Looping over a list [closed]
I'm new to Python, and I have a small section of code which works fine when outside of a function:
list = [["M4342","Joe"],["M4343","Melanie"],["M4344","Alex"],["","David"]]
for value in list:
...
4
votes
2answers
80 views
Idiomatic loop and break condition
I am calling a C library via P/Invoke. That library needs to be called in sucession with an increasing unsigned int, and returns a pointer. That pointer is then mapped to a managed type, until it ...
1
vote
2answers
78 views
Optimization of a while-loop searching for words in a dictionary
This is my first question here. I'm using an open source program called MElt which lemmatize (give the lemma example:giving-->give) of words. MElt works on Linux and its programmed in Perl and ...
2
votes
2answers
67 views
Change one column data from List<>
I have this code:
for (int i = 0; i < lst.Count(); i++)
{
lst[i].ColumnOrder = (short)i;
}
But I was trying to find a way to do it via LINQ or something else with better performance. I'm ...
0
votes
1answer
51 views
Avoiding dynamic RegEx creation in JavaScript
This function's job is to replace several text smilies, e.g. :D, :), :love with the appropriate smiley image.
In my opinion my code has several issues, yet the main problem is that a lot of quite ...
1
vote
1answer
101 views
Optimising If statement and for loops
I have two solutions (in jython) to take two pictures and copy and paste alternate sections of one picture to the other, like so:
for x in range (0,w):
for y in range (0,h):
...
1
vote
1answer
60 views
Is there any better way to fetch two tables in one statement or query?
The 'Do' method is for prepared statements and returns (by fetchall()). I am also using two looping statements. Is there any better way to fetch two tables in one statement or query? If not, what ...
1
vote
2answers
44 views
Object iteration and appending to form
In this code, I'm getting the json from backend. Once I find the json availability, I loop and make 5 columns. Once the columns are made, I append after the legend of my form.
Can this be improved ...
2
votes
4answers
278 views
Simplify Code — For Each Loop
I am trying to return a value (a.tnAddress) from a custom class based on a lookup (foreach loop). Depending on the type of transaction, I will need to do the foreach loop based on different ...
1
vote
2answers
103 views
Is there a better, more efficient way to write this loop?
While working with someone else's code I've isolated a time-killer in the (very large) script. Basically this program runs through a list of thousands and thousands of objects to find matches to an ...
0
votes
0answers
21 views
The object iteration having static values and lengthy codes
I am converting a existing object in to new object to fit for handlebarsJs.
the loop func i use is here:
var mainLink = {"links":[]}; x = 0;
//mainLin is the final object
for(key ...
1
vote
2answers
74 views
Optimizing Java code for setting color of specific keywords in a textview
I'm developing an Android app to view C-programs.I wanted to give a simple color scheme to the text which is stored in database,retrieved as string and then passed on to the textview.
The code I ...
2
votes
2answers
88 views
Refactoring same loop logic with different input data
I have these two methods:
public static String buildLine(String[] values) {
StringBuilder lineBuilder = new StringBuilder();
for (int i = 0; i < values.length - 1; i++) {
...
1
vote
1answer
71 views
Video/article slider.
I have created a video/article slider. It works fine, but I have a feeling it's not quite a standard solution.
I would appreciate it if someone could review my code, so that if there are mistakes, I ...
2
votes
2answers
48 views
I'm practicing turning for loops into recursive functions. what do you think?
I am trying to turn this function:
collection = ['hey', 5, 'd']
for x in collection:
print(x)
Into this one:
def printElement(inputlist):
newlist=inputlist
if len(newlist)==0:
...
0
votes
1answer
26 views
Should I use and loop, or just a loop?
I build a code like this
if(listObj.Any(x => x.id < 0))
{
foreach(ModelClass item in listObj)
{
if(item.id < 0)
{
// code to create a new Obj in the ...
2
votes
1answer
44 views
Tcl loop idea: for_index_item
I often need to loop through a list of items and need both the index position, and the item itself. Typically:
set names {John Paul George Ringo}
set i 0
foreach name $names {
puts "$i - $name"
...
1
vote
2answers
84 views
A replacement for this infinite loop?
/**
* Returns returns a pseudo-random Gaussium number. Forumla:
* {@code (RanGassium * 3) + mean }</ br> 3 is hardcoded for 97% accuracy.
*
* @param mean
* mean of returning ...
2
votes
1answer
91 views
Review a simple sprite sheet animation script
This is just a simple script for testing sprite sheet animations. I'm trying to improve my code style, so I'm curious about suggestions anyone might have about best practices or readability.
...
7
votes
2answers
210 views
What's the most idiomatic way to call a function an arbitrary number of times with the previous result?
Basically I have a function of the type 'a -> 'a (an optimization function on a AST) and I want to call it (passing the previous result) until it returns the same thing as the input.
Right now I ...
1
vote
2answers
71 views
Making a 2D dict readable
I am building a 2d dictionary with an altered list at the end Afterwords I want to find the min of each list. Is there a better way to do it? Better being faster or at least less of an eyesore.
...
-1
votes
3answers
109 views
dead code and infinite loops with java for loop [closed]
I have a for loop that loops infinity when i don't breakout of it and only loops once when i do break out of it. I'm sure im missing something simple.
The if and else should be identical aside from ...
1
vote
0answers
37 views
Ascii Table in brainfuck
I made my first brainfuck program today, which prints the numbers 0-255 and the corresponding character.
I was wondering if I could improve my program, as I'm repeating my self a lot (eg. 3 x copy ...
1
vote
1answer
37 views
Matlab: speeding up repetitive initialisation with structured data?
I need to speed up this code: it creates a multilinear function. I considered the below methods but no idea which with the largest benefit.
replacing dummy variables with things such as ...
0
votes
1answer
36 views
Range of all variables in List in Prolog
I have a list in Prolog like this:
Puzzle = [ A1, A2, A3,
B1, B2, B3,
C1, C2, C3 ].
And I want every variable in the list to be a number in the range of 1 to 9.
Currently ...
1
vote
1answer
86 views
High School Java Class: Pong Project External Reviewer
Panelball class:
import java.awt.*;
import java.awt.event.KeyEvent;
import javax.swing.*;
public class Panelball extends JPanel implements Runnable {
private static final long ...
1
vote
3answers
110 views
How to flatten the nested for loops?
The problem I am facing is:
I need to interate through a bunch of lists, and there are separated conditions which needs to be satisfied by the list. conditons are not independent.
I care about the ...
3
votes
1answer
97 views
Iterate through a LinkedList in a cleaner manner
I have a collection of PlaylistItem objects. They are linked together such that each item knows the ID of its next/previous item. I am iterating over this collection of objects, starting at a known ...
0
votes
1answer
64 views
Improvment of and looping in a regular expression pattern
My implemented regex pattern contains two repeating symbols: \d{2}\. and <p>(.*)</p>. I want to get rid of this repetition and asked myself if there is a way to loop in Python's regular ...
2
votes
1answer
115 views
Optimization expression evaluation chalange
It's my first message on this site. I'm trying to evaluate/learn C#. I'm an old (C++, MFC) programmer.
I'm trying to learn C# with this little program(game) for my children.
I'm trying to calculate ...
1
vote
1answer
41 views
Return most common items in list (php)
I was wondering what's the best way to return the most common items in a list, so far the best way I could do it was:
//@param string $this->matched_ids_list = 1,11,12,11,12,
...
4
votes
2answers
227 views
Does this simple C++ program need any further improvement?
First of all I'm a complete noob when it comes to programming. So please forgive me if I unwillingly say something stupid. Now this is my first program written in C++ that I chose to make public in ...
1
vote
0answers
80 views
new md5 algorithm
can you please try my algorithm and give a comment or feedback about it? thank you in advance.. :) this is in java language just copy and paste all of it and run in e.g. netbeans or eclipse thanks
...
5
votes
4answers
415 views
Improve VBA code Excel
Im looking for someone who can improve my code, because at the moment it takes 4 minutes to execute. I'm new to VBA so it's probably bad coding. Normally if you are good in VBA you will understand the ...
0
votes
1answer
91 views
Proper use of reduce, nested loops
Below is an implementation of Dijkstra's shortest path algorithm. It's input graph is represented as an association list of source nodes to assoc of target node and arc weight.
My question is about ...
4
votes
3answers
389 views
How to increase the performance of loop in Python?
I need to run over a log file with plenty of entries (25+GB) in Python to extract some log times.
The snippet below works.
On my test file, I'm ending up with roughly 9:30min (Python) and 4:30min ...
2
votes
1answer
91 views
Vectorize or otherwise speed up a double for loop
I have this function containing some loops and a double for loop. What it does is set up a matrix first to store results in (the task at hand is comparing genomes) and then with sliding window, ...
3
votes
4answers
114 views
Remove code duplication inside of a loop preserving performance
I'm coding a 2D collision engine, and I need to merge adjacent axis-aligned bounding boxes depending on a direction (left, right, top, bottom).
The four cases are very similar, except for the if ...
4
votes
1answer
132 views
Looping through Radgrid in JS is slow, can this be faster?
I do a single select in one radgrid, and based upon that selection I want to select multiple rows in a different radgrid:
<telerik:RadGrid ID="rgPaymentLines"> <ClientEvents ...
1
vote
1answer
71 views
Too many variables?
This is my first C++ program and I was wondering if I'm using too many variables. Its a simple program that gets a number from a user and calculates the number of primes between 3 and the user's ...
1
vote
2answers
72 views
Setting a OnClickListener in a loop
I am trying to set a OnClickListener to a image in a loop. If the params platform is "android" then use market app, instead of default browswer. Is there a better solution to my exception handling or ...
3
votes
1answer
47 views
Code to find the proper index comparing 3 values for max one
I have an algorithm and the idea is to move over an array choosing as index the index of the neighboring cell that has the max value.
I.e.
if array[i + 1][j + 1] has the largest value among the 3 ...
1
vote
1answer
122 views
PHP - is my code efficient?
I am basically getting data from various APIs and using PHP to put them together - like a web mashup. I am currently using 4 foreeach statements to insert the gathered data into their individual ...
4
votes
5answers
237 views
Want to remove the goto in my code
I want to remove my goto's in this code but I'm not sure how. There must be a cleaner way to write my code. Perhaps move the "if" statement to the end of each loop then use break?
Sorry if this is a ...
1
vote
2answers
76 views
A program that mimics T9 cell messaging
Today I solved a question from Google Code Jam. A program that mimics a cell phone keypad messaging.
Here is the program, with goto statement, I know it is unstructured style to use goto, but I still ...
0
votes
1answer
58 views
I have 2 errors about syntax that i can't find. it's in if else loop [closed]
I have 2 errors in the if else loop and i don't konow what? Thanks for help me. I am a beginnner.
#include <cs50.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
...
1
vote
1answer
75 views
Django how to make this view faster?
I have view and it works correct, but very slow
class Reading(models.Model):
meter = models.ForeignKey(Meter, verbose_name=_('meter'))
reading = models.FloatField(verbose_name=_('reading'))
...