I recently started to do some of the exercises in CodeChef and I did the TurboSort. It works fine with smaller arrays but can't pass some of the test due to the fact it's \$O(n^2)\$, so I need faster way to sort them.
import java.util.Scanner;
public class TurboSort {
public static void m...
The newsletter suggested I try to answer this, so here is my attempt:
This looks like a lot of repeated code here. I don't think this can be shortened really, but I would create a function to do the conversion in:
switch(x)
{
case '"': cont.push_back('"'); break;
case '\\': con...
So, I just did this brainfuck interpreter.
I have some concern, like the "updateValue" function, I was trying to follow the functional paradigm, but I wonder if I could use another approach or something.
my parsing function seems overly complicated, the '[' case seems complicated
Any other com...
some think ++i is faster, cause it doesn't have to save the previous state in when it is used. however newer compilers should generate the same assembly for both cases
I'm trying to design the classes for the Wheel of Fortune game in java. Below diagram represents the classes and the interaction between them.
.
Below is the partial implementation of it. I appreciate some feedback on my design and approach. I'm mainly interested in classes and their dependenc...
@rolfl I upvoted your question but I wouldn't even know it was Java if you did not tell me :) — Koray Tugay5 hours ago
I would assume compiler would optimize it. Actually I will check the byte code generated. I forgot it static, thanks for the great feedback. — Koray Tugay6 hours ago
@JaDogg Not necessarily, you do know that i++ will increment the variable after it is used in the statement, and ++i will increment it before, don't you?
So, std::cout << i++ is not the same as std::cout << ++i
@Hosch250 - Java is very careful to specify the input and output of it's language keywords/operators. It is very careful to NOT specify the implementation.
how it gets implemented under the covers is different from one implementation to the next, and also from one method call to the next, and, often, code is compiled multiple different ways in different places.....
Also, a function A could be interpreted the first 100 times it is called, then, the next 100 times it can be compiled with a "quick compile, not many optimizations applied"... then,
the next 1000 times it uses yet another compile with different optimizations...
then, it may find that certain times and from certain places, the same inputs are always supplied, so it may turn the function in to a constant for a specific call-path.
The simple-to use, but generic timeout class to be used watching network connections, user input, filesystem events, and is intended to have a very simple interface specific to our use cases.
Intended steps to use:
Construct
Activate
Potentially react to a timeout, deactivate
Re-activate
Destr...
Java source code is 'compiled' in to class files, which are byte code which represent the logic that is to be applied. The Java runtime can interpret the byte-code at runtime.
then, if some bytecode is used a lot, it may compile that down to machine code, and execute, instead of interpret it.
it means, when you run java applications, that the important methods (that are used a lot), get compiled with the absolute best optimizations applied, including runtime metrics that can affect optimization, for the specific hardware and versions of libraries you have available.
yes, but, it compiles to the particular machine code that you specify at compile time, and, unless you do magic, it cannot apply all the possible optimizations that Java can.
If you want to take advantage of the special CPU features on an intel cpu, you have to compile it for that, and if you want it to use special AMD functions, you have to compile it for that....
Now, in C++ , the ++i vs i++ argument is entrenched... and it holds more water. In Java, it is moot, and who cares. The way the code actually compiles down is 'opaque', so there's not much point in looking too deep.
If someone can review this C#/Linq grouping code snippet please.
I have a list of Options. From that list
first I want to get only Drink (the where clause)
Then I want to group them by their SortOrder(range of 100)
I can stop there and loop through the items and create checkboxes for each ite...
I have written this java snippet to generate all permutations of a given array of integers. Can somebody review it and give some pointers as to how to make it more generic, i.e. instead of dealing with only ints, it should be able to handle a collection of any data type. Thanks.
import java.util...
He keeps/has kept fish, he plays the piano, he is presumably a wookworker seeing he is part of the private beta, he is knowledgable on Physics, and lots of other things.
I'll be lucky if I'm half as knowledgeable as him when I'm his age.
Executive summary: No.
i++ could potentially be slower than ++i, since the old value of i
might need to be saved for later use, but in practice all modern
compilers will optimize this away.
We can demonstrate this by looking at the code for this function,
both with ++i and i++.
$ cat i++.c
ex...
We looked at this answer for C in this question:
http://stackoverflow.com/questions/24886/is-there-a-performance-difference-between-i-and-i-in-c
What's the answer for C++?
Is there any performance difference between using something like
for(int i = 0; i < 10; i++) { ... }
and
for(int i = 0; i < 10; ++i) { ... }
or is the compiler able to optimize in such a way that they are equally fast in the case where they are functionally equivalent?
Edit:
This was asked...
In java I usually make a for-loop like following:
for (int i = 0; i < max; i++) {
something
}
But recently a colleague typed it so:
for (int i = 0; i < max; ++i) {
something
}
He said the latter would be faster. Is that true?
++i will increment the value of i, and then return the incremented value.
i = 1;
j = ++i;
(i is 2, j is 2)
i++ will increment the value of i, but return the original value that i held before being incremented.
i = 1;
j = i++;
(i is 2, j is 1)
For a for loop, either works. ++i seems m...
According to this, it is different because it can't just return the same value - it has to create a temporary value. However, most compilers will just reduce it to the same code if it doesn't matter if it isn't used or assigned as part of the statement.
It also heavily depends on whether i is an int or a custom type.
It is more important to use ++i on custom types than it is for ints because custom types are typically more resource intensive.
@Hosch250 This, however, is for c, and compilers for C++ apparently cannot optimize it the same.
++ Yes, there is no difference, and even if there were, the loop would have to be almost empty of code to notice the difference. In this particular code, the loop overhead is to the loop contents as "a lightning bug is to lightning". (Mark Twain quote) — Mike DunlaveyMay 8 '09 at 18:32
For a Pascal, Delphi, Pascal Script related question: Pascal / Delphi / Pascal Script tags
What about create a Free Pascal tag?
Object Free Pascal (as in Free Pascal compiler), as far as I know, is given by the directive:
{$mode objfpc}{$H+}
The compiler supports Delphi as well, but not co...
I have an application that provides the user the ability to backup the local SQLite database to another location. I then provide the ability to restore the backup database and overwrite the current database with the backup copy. In my restore command, I make a backup of the database file, which...
I'm preparing for an interview. I tried implementing binary search tree in c++. I used array but it seems to be complicated to restructure the array while deleting nodes. Is linked list a better data structure to implement bst? Also please let me know if the code has bad coding practice like memo...
I'm currently doing an algorithms course and implemented this version of the quicksort algorithm. I would like to know if it is an efficient implementation. I've seen a couple others where there is a partition function that partitions the list is that a superior method to what I did ? Thanks in a...
I have this code which mimics a rugby match. You select a player by clicking the div (turns green), move it, and click to release( returns to default color). There is a bug, the mouse can move too fast, thus leaving the div behind. I am also trying to make it OO, but I'm having problems as you ca...
The first line of the input contains the total number of stack operations N, 0 < N ≤ 100000. Each of the next N lines contains a description of a stack operation, either in the form PUSH A B (meaning to push B into stack A), or in the form POP A (meaning to pop an element from stack A), where A i...
Hi can i ask the time complexity for the code below? My answer is n*size_of_list
def sort_it(lst):
if is_empty_tree(lst):
return []
else:
c = []
for i in lst:
c = insert_tree(i , c)
b = flatten(c)
return b
my flatten is time complexity...
Click the divs to remove them. Remove all div's in the shortest time and minimal clicks (to do). Code needs streamlining.
<!DOCTYPE html>
<html>
<head>
<style>
div {
width:30px;
height:30px;
background-color:blue;
position:fixed;
}
</style>
<script>
function init() {
var i;
count=10;
for (i=0;i
I'm writing a C (c99) library that operates on strings. I'm having a design problem writing functions that will perform in different modes.
For example; the Find function can search for:
the first, last, n-th element
different letter case
return position before or after the found element
sear...
So I'm writing this program that receives the input from mysql it compiles correctly but just once. Python can't do {} while could anybody help because even two while true loops did not do the trick ?
!/usr/bin/env python
import RPi.GPIO as GPIO
import time
import MySQLdb
GPIO.setmode(GP...
Devise an algorithm that will operate on a number x and a set of n distinct numbers such that in O(nlgn) time, the algorithm indicates whether or not there are two numbers in the set that have a product of x. Explain why your algorithm works.
My algorithm:
Step 1: Sort the set // O(nlgn)
Step ...
Basic Info
I needed a lock-free ringbuffer with multiple readers (but one writer). However, I did not want the writer to check all readers every time in order to prevent an overrun. Thus, I decided to split the buffer in two halves. If the write pointer is in one half, it is only allowed to adva...
To see how any formula was written in any question or answer, including this one, right-click on the expression it and choose "Show Math As > TeX Commands".
For inline formulas, enclose the formula in $...$. For displayed formulas, use $$...$$. These render differently: $\sum_{i=0}^n i^2 = \fr...
I have sample graphs like the following(Un-directed un-weighted cyclic graphs). My goal is to find shortest path between a given source and destination.
film--->[language, film_actor, film_category, inventory]
film_actor--->[actor, film]
store--->[customer, inventory, staff, address]
payment--->...
Hiya @nhgrif! Just dropped in to poke you about your blog :p I saw it thru a retweet on twitter, it was exemplary and I like it a lot (even added it the feeds of my home room). I'm just asking if you'll be licensing your blog entries, because I'd like to quote them in the future. TIA
@Unihedro You're welcome to quote them all you want. Just be sure to always link back to the one you're quoting, and if you're doing it on Twitter, mention my Twitter account.
Let's look at tableView:cellForRowAtIndexPath:. This needs to be abstracted quite a bit more.
First of all, CustomTableViewCell is a bad name for a class. Every table view cell is custom. We need to spend more time working on our cell subclass, because the name is just the start.
Let's chang...
@skiwi not so serious for how we have used it so far. but when I now wanted to do some other code stuff after having done a benchmark, it didn't go so well...
earlier we've only used it once, and then finished the Java program
the bug occurs, and is serious, when you want to do more...
Can anyone kindly help me to improve it? Many thanks.
def comp( nums,i,j,exps,op):
nums0=[];
for x in range(0,len(nums)):
if not (x==i or x==j):
nums0.append(nums[x])
if(op=='+'):
tmp = nums[i]+nums[j];
elif(op=='*'):
tmp = nums[i]*nums[j];
elif(op=='-'):
tmp = nums[i]-n...
perhaps, but, using the OS's free report to debug the problem is not the solution. Add -verbose:gc to your tomcat startup, and read the GC reports. Then, attach a jvisualvm to it to see where the memory is leaking.....
I need to power-off my basement server, and take a vacuum-cleaner and air-blaster to it.
I have written the following service class for my application and at the moment it only contains a single method which is responsible for carrying out the necessary actions to add a new team to the database - more methods will be added later but I want to make sure that I'm on the right track wit...
I am trying to implement A* search on a grid in MATLAB. I am currently using a priority queue class I found here, but it's a bit slow. I tried to write this simple priority queue class in MATLAB:
classdef PQ < handle
properties
nElements;
priorityList;
valueList;
...
@rolfl well, the server isn't that important really. I mess with it sometimes and I do not in any way guarantee 100% uptime.
but this OOME thing has been a hard one. Last time I tried to debug it, I only came to the conclusion that it was Strings and char arrays that crowded the server.
I guess the problem is with the fetching of the comments, but I think Java should garbage collect those objects properly.
What is "project 14" of Project Euler ask - what does this code do? Also, if this code works, you might be better of posting to Code Review than SO — en_Knight42 secs ago
@rolfl "enough" I would say. Don't know all the inner workings of it, but it is recommended to re-use them and Jackson is a well-known and well-used library so I don't think that's the problem.
when doing some debugging for something a while ago, I changed so that some messages where put to a list so that I could read them by visiting a website. But I did delete that (of course that was an obvious memory sink)
@rolfl another possible sink that I have been thinking about is the .dao package and Hibernate. Wouldn't be surprised if Hibernate is eating memory somewhere
@Simon - the only leak-concerns I see in the commentscan code is the mechanize and mapper instances. I don't see any hibernate code in there... what am I missing?
The github code looks .... complicated... I have not followed it all through yet.
@rolfl there is no hibernate code in the comments scan code. The hibernate code is related to @Duga's other reposinsibilities, such as keeping track of the number of commits and issues per day.
21-Mar-2015 19:50:23.506 SEVERE [http-nio-80-ClientPoller-0] org.apache.tomcat.util.net.NioEndpoint$Poller.run java.lang.OutOfMemoryError: Java heap space