예제 #1
0
    private static void PrintAddMethodsComparison()
    {
        Console.Write("{0, -30}", "Sum of ints: ");
        DisplayExecutionTime(() =>
        {
            AddMethods.AddInt(1, 2);
        });

        Console.Write("{0, -30}", "Sum of longs: ");
        DisplayExecutionTime(() =>
        {
            AddMethods.AddLong(1, 2);
        });

        Console.Write("{0, -30}", "Sum of floats: ");
        DisplayExecutionTime(() =>
        {
            AddMethods.AddFloat(1, 2);
        });

        Console.Write("{0, -30}", "Sum of doubles: ");
        DisplayExecutionTime(() =>
        {
            AddMethods.AddDouble(1, 2);
        });

        Console.Write("{0, -30}", "Sum of decimals: ");
        DisplayExecutionTime(() =>
        {
            AddMethods.AddDecimal(1, 2);
        });
    }
예제 #2
0
        public double Operate(IOperation operation, double[] operands)
        {
            double result = 0;

            switch (operation.Name)
            {
            case "+":
                foreach (var addMethod in AddMethods.Where(addMethod => addMethod.Metadata.Speed == Speed.Fast))
                {
                    result = addMethod.Value(operands[0], operands[1]);
                }
                break;

            case "-":
                result = Subtract(operands[0], operands[1]);
                break;

            case "/":
                result = operands[0] / operands[1];
                break;

            case "*":
                result = operands[0] * operands[1];
                break;

            default:
                throw new InvalidOperationException(
                          String.Format("invalid operation {0}", operation.Name));
            }

            return(result);
        }
            private ProgramState ProcessInvocation(ProgramState programState, InvocationExpressionSyntax invocation)
            {
                // Argument of the nameof expression is not pushed on stack so we need to exit the checks
                if (invocation.IsNameof(semanticModel))
                {
                    return(programState);
                }

                // Remove collection constraint from all arguments passed to an invocation
                var newProgramState = RemoveCollectionConstraintsFromArguments(invocation.ArgumentList, programState);

                var memberAccess = invocation.Expression as MemberAccessExpressionSyntax;

                if (memberAccess == null)
                {
                    return(newProgramState);
                }

                var collectionSymbol = semanticModel.GetSymbolInfo(memberAccess.Expression).Symbol;
                var collectionType   = GetCollectionType(collectionSymbol);

                // When invoking a collection method ...
                if (collectionType.IsAny(TrackedCollectionTypes))
                {
                    var methodSymbol = semanticModel.GetSymbolInfo(invocation).Symbol as IMethodSymbol;
                    if (IsIgnoredMethod(methodSymbol))
                    {
                        // ... ignore some methods that are irrelevant
                        return(newProgramState);
                    }

                    if (methodSymbol.IsExtensionMethod)
                    {
                        // Extension methods could modify the collection, hence we remove the Empty constraint if present
                        return(collectionSymbol.RemoveConstraint(CollectionCapacityConstraint.Empty, newProgramState));
                    }

                    if (AddMethods.Contains(methodSymbol.Name))
                    {
                        // ... set constraint if we are adding items
                        newProgramState = collectionSymbol.SetConstraint(CollectionCapacityConstraint.NotEmpty,
                                                                         newProgramState);
                    }
                    else
                    {
                        // ... notify we are accessing the collection
                        OnCollectionAccessed(invocation,
                                             collectionSymbol.HasConstraint(CollectionCapacityConstraint.Empty, newProgramState));
                    }
                }

                return(newProgramState);
            }
            private ProgramState ProcessInvocation(ProgramState programState, InvocationExpressionSyntax invocation)
            {
                var newProgramState = RemoveCollectionConstraintsFromArguments(invocation, programState);

                var memberAccess = invocation.Expression as MemberAccessExpressionSyntax;

                if (memberAccess == null)
                {
                    return(newProgramState);
                }

                var collectionSymbol = semanticModel.GetSymbolInfo(memberAccess.Expression).Symbol;
                var collectionType   = GetCollectionType(collectionSymbol);

                // When invoking a collection method ...
                if (collectionType.IsAny(TrackedCollectionTypes))
                {
                    var methodSymbol = semanticModel.GetSymbolInfo(invocation).Symbol as IMethodSymbol;
                    if (IsIgnoredMethod(methodSymbol))
                    {
                        // ... ignore some methods that are irrelevant
                        return(newProgramState);
                    }

                    if (AddMethods.Contains(methodSymbol.Name))
                    {
                        // ... set constraint if we are adding items
                        newProgramState = collectionSymbol.SetConstraint(CollectionCapacityConstraint.NotEmpty,
                                                                         newProgramState);
                    }
                    else
                    {
                        // ... notify we are accessing the collection
                        OnCollectionAccessed(invocation,
                                             collectionSymbol.HasConstraint(CollectionCapacityConstraint.Empty, newProgramState));
                    }
                }

                return(newProgramState);
            }